Setting the Unity Texture Name

Asked 2 years ago, Updated 2 years ago, 107 views

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class BGScrollData
{
    public Renderer RenderForScroll;
    public float Speed;
    public float OffsetX;
}
public class BGScroller : MonoBehaviour
{
    [SerializeField]
    BGScrollData[] ScrollDatas;

    // // Start is called before the first frame update
    void Start()
    {

    }

    // // Update is called once per frame
    void Update()
    {
        UpdateScroll();
    }

    void UpdateScroll()
    {
        for(int i = 0; i < ScrollDatas.Length; i++)
        {
            SetTexturOffset(ScrollDatas[i]);
        }
    }

    void SetTexturOffset(BGScrollData scrollData)
    {
        scrollData.OffsetX += (float)(scrollData.Speed) * Time.deltaTime;
        if (scrollData.OffsetX > 1)
            scrollData.OffsetX = scrollData.OffsetX % 1.0f;

        Vector2 Offset = new Vector2(scrollData.OffsetX, 0);

        scrollData.RenderForScroll.material.SetTextureOffset("_MainTex", Offset);
    }
}

I just started studying UNI.T. By the way, while studying background scroll,

void SetTexturOffset (BGScrollData)
    {
        scrollData.OffsetX += (float)(scrollData.Speed) * Time.deltaTime;
        if (scrollData.OffsetX > 1)
            scrollData.OffsetX = scrollData.OffsetX % 1.0f;

        Vector2 Offset = new Vector2(scrollData.OffsetX, 0);

        scrollData.RenderForScroll.material.SetTextureOffset("_MainTex", Offset);
    }

If a string other than "_MainTex" is included as a parameter of SetTextureOffset at the end, an error occurs. The instructor didn't explain why I chose that string.Let me ask you a questionㅠ<

unity c#

2022-09-20 19:34

1 Answers

SetTextureOffset's "_MainTex" is a parameter that represents the method according to the texture type

Perhaps in that lecture, you can see that you scroll through the background using the default metal


2022-09-20 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.