Unity Custom Inspector 커스텀 인스펙터

Unity/찾아본것 2019. 7. 22. 16:14



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;
 
public class Cube : MonoBehaviour
{
    void Start()
    {
        GenerateColor();
    }
 
    public void GenerateColor()
    {
        this.GetComponent<Renderer>().sharedMaterial.color = Random.ColorHSV();
    }
 
    public void Reset()
    {
        this.GetComponent<Renderer>().sharedMaterial.color = Color.white;
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using UnityEngine;
using UnityEditor;
 
[CustomEditor(typeof(Cube))]
public class CubeEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
 
        Cube cube = (Cube)target;
 
        GUILayout.BeginHorizontal();
 
        if (GUILayout.Button("GenerrateColor"))
        {
            cube.GenerateColor();
        }
 
        if (GUILayout.Button("ResetColor"))
        {
            cube.Reset();
        }
 
        GUILayout.EndHorizontal();
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Sphere : MonoBehaviour
{
    public float baseSize = 1f;
 
    void Update()
    {
        float anim = baseSize + Mathf.Sin(Time.time * 8f) * baseSize / 7f;
        transform.localScale = Vector3.one * anim;
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using UnityEditor;
 
[CustomEditor(typeof(Sphere))]
public class SphereEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
 
        Sphere sphere = (Sphere)target;
 
        sphere.baseSize = EditorGUILayout.Slider(sphere.baseSize, .1f, 2f);
        sphere.transform.localScale = Vector3.one * sphere.baseSize;
    }
}
 
cs



왜 sphere..가 같이 색이 변하지 ( ? )

: