I created a custom inspector where I assign various objects and values but if I unselect the object the values are lost.
[CustomEditor(typeof(WeaponControl))]
[System.Serializable]
public class WeaponInspector : Editor {
public WeaponControl _myTarget;
public AudioClip audioClip;
public GameObject parentGO;
[SerializeField]
public GameObject Character;
public GameObject weaponPrefab;
public GameObject LeftHand;
private GameObject RightHand;
private Vector3 copyPosition;
public static void SetupScene()
{
EditorApplication.NewEmptyScene();
}
private void OnEnable()
{
Debug.Log("OnEnable was called...");
_myTarget = (WeaponControl)target;
}
private void OnDisable()
{
Debug.Log("OnDisable was called...");
}
private void OnDestroy()
{
Debug.Log("OnDestroy was called...");
}
public override void OnInspectorGUI()
{
//DrawDefaultInspector();
DrawWeaponBuilder();
}
private void DrawWeaponBuilder()
{
//Weapon type
_myTarget.weaponType = (WeaponManager.WeaponType)EditorGUILayout.EnumPopup("Type of weapon: ", _myTarget.weaponType);
EditorGUILayout.BeginVertical("box");
//Weapon prefab
weaponPrefab = (GameObject)EditorGUILayout.ObjectField("Weapon prefab: ", weaponPrefab, typeof(GameObject), false);
if (weaponPrefab != null&& RightHand!=null)
{
if (GUILayout.Button("Add Weapon to scene"))
{
GameObject weapon= (GameObject) Instantiate(weaponPrefab, RightHand.transform.position, RightHand.transform.rotation);
weapon.transform.parent = parentGO.transform;
}
}
//Audioclip, must be added to AudioSource component
audioClip = (AudioClip)EditorGUILayout.ObjectField("Audio Clip", audioClip, typeof(AudioClip), false);
if (audioClip != null)
{
if (GUILayout.Button("Add Audio Source component"))
{
parentGO.AddComponent<AudioSource>();
parentGO.GetComponent<AudioSource>().clip = audioClip;
}
}
//Position target forr left hand
_myTarget.HandPosition = (GameObject)EditorGUILayout.ObjectField("Hand Position: ", _myTarget.HandPosition, typeof(GameObject), false);
//Prefab for ParticleSystem
_myTarget.bulletPrefab = (GameObject)EditorGUILayout.ObjectField("Bullet Particles: ", _myTarget.bulletPrefab, typeof(GameObject), false);
//Where the bullets spawn
_myTarget.bulletSpawn = (Transform)EditorGUILayout.ObjectField("Bullet Spawn: ", _myTarget.bulletSpawn, typeof(Transform), false);
EditorGUILayout.EndVertical();
// Max ammo for this weapon
_myTarget.MaxAmmo = EditorGUILayout.IntField("Max # of rounds: ", _myTarget.MaxAmmo);
//Max # of clips the player can carry
_myTarget.MaxClipAmmo = EditorGUILayout.IntField("Max # clips: ", _myTarget.MaxClipAmmo);
if (LeftHand != null)
{
_myTarget.EquipPosition = EditorGUILayout.Vector3Field("Equip position: ", LeftHand.transform.position);
}
else
{
_myTarget.EquipPosition = EditorGUILayout.Vector3Field("Equip position: ",new Vector3(0,0,0));
}
if (weaponPrefab != null)
{
if (GUILayout.Button("Copy Transform"))
{
CopyTransform(weaponPrefab);
}
}
if (weaponPrefab != null)
{
if (GUILayout.Button("Past Transform"))
{
PastTransform(weaponPrefab);
}
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
Character = (GameObject)EditorGUILayout.ObjectField("Character: ", Character, typeof(GameObject), false);
if(Character!= null)
{
if(GUILayout.Button("Add Character"))
{
Instantiate(Character, Vector3.zero, Quaternion.identity);
}
}
if (Character != null)
{
RightHand = (GameObject)EditorGUILayout.ObjectField("RightHand: ", RightHand, typeof(GameObject), true);
}
if (Character != null)
{
LeftHand = (GameObject)EditorGUILayout.ObjectField("LeftHand: ", LeftHand, typeof(GameObject), true);
}
}
private void CopyTransform(GameObject go)
{
copyPosition = go.transform.localPosition;
}
private void PastTransform(GameObject go)
{
weaponPrefab.transform.localPosition= copyPosition;
}
}