Unity 切换烘培贴图脚本

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

public class LightControl : MonoBehaviour
{
    MeshRenderer MeshRenderer;
    //灯光信息
    LightmapData[] lightmapData;
    LightmapData lmd;
    [SerializeField] bool _isTurnOn; //是否开灯

    private void Awake()
    {
        MeshRenderer = GetComponent<MeshRenderer>();
        lightmapData = LightmapSettings.lightmaps;
        lmd = new LightmapData();
    }

    private void Start()
    {
        _isTurnOn = true;
    }

    public void TurnOnLight() 
    {
        if (_isTurnOn)
        {
            SetLightMap(LightManager.Instance.SecondLightColor[MeshRenderer.lightmapIndex], MeshRenderer.lightmapIndex);
            _isTurnOn = false;
        }
        else
        {
            SetLightMap(LightManager.Instance.FirstLightColor[MeshRenderer.lightmapIndex], MeshRenderer.lightmapIndex);
            _isTurnOn = true;
        }
    }

    public void SetLightMap(Texture2D lightmapTex, int index)
    {
        if (lightmapTex == null)
        {
            return;
        }
        lmd.lightmapColor = lightmapTex;
        lightmapData[index] = lmd;
        LightmapSettings.lightmaps = lightmapData;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public struct TurnOnLightBtn 
{
    public Button BtnTurnOnLight;
    public List<LightControl> LightControl;
}
public class LightManager : Singleton<LightManager>
{
    public Texture2D[] FirstLightColor;   //第一套灯光贴图
    public Texture2D[] SecondLightColor;   //第二套灯光贴图

    public List<TurnOnLightBtn> _TurnOnLight;
    void Start()
    {
        for (int i = 0; i < _TurnOnLight.Count; i++)
        {
            //_TurnOnLight[i].BtnTurnOnLight.onClick.AddListener(TurnOnLight);
        }
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Test")) 
        {
            print(_TurnOnLight.Count);
        }
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_46711336/article/details/128593497