Unity物体透明度设置

  • TransparencySeter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransparencySeter : MonoBehaviour {

    Renderer sr;

    // Use this for initialization
    void Start () {
        sr = GetComponent ();
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public void SetAlpha(float alpha){
        if(!sr)
            sr = GetComponent ();

        if (sr) {
            var c = sr.material.color;
            c.a = alpha;
            sr.material.color = c;
        }
    }

    public float GetAlpha(){
        if(!sr)
            sr = GetComponent ();
        
        if (sr) {
            Debug.Log ("GetAlpha");
            return sr.material.color.a;
        }
        else
            return 1.0f;
    }
}

  • TransparencyEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(TransparencySeter))]
public class TransparencyEditor : Editor {
    float Alpha = 1.0f;
    TransparencySeter setter;

    void OnEnable(){
        setter = (TransparencySeter)target;
        Alpha = setter.GetAlpha ();
        Debug.Log (Alpha);
    }

    public override void OnInspectorGUI(){
        DrawDefaultInspector ();
        Alpha = EditorGUILayout.Slider ("Alpha", Alpha, 0, 1);
        if (GUI.changed) {
            setter.SetAlpha (Alpha);
        }
    }
}

你可能感兴趣的:(Unity物体透明度设置)