- Home /
Custom Editor applies changes to every instance of the component/class its for, how do I fix this to modify the target instance component only?
Hi Everyone,
Background: Recently I started dabbing in using the custom editor to organize the data I had in my class (in this case a class for Limbs.) The interface works as it's supposed to however I realized when I apply the script as a component to a game object and then add the script as a component to a completely separate object the stats between them will be the same and changing the stats in one will change them all.
Question: I would like to emulate how Unity, by default, keeps the data of the same component type in two different objects as different, how can I accomplish this?
Demonstration of error, note that in VALimb the "Name" is the same in the Cube and Capsule object. The New Behaviour Script has no custom editor and maintains that "NAME" (in this case) is different between the two objects, though it's the same component type.
Thanks, SZ95
Code of VALimb Custom Editor:
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(VALimb))]
public class VALimbEditor : Editor
{
private static bool TorsoToggle;
private static bool ArmToggle;
private static bool LegToggle;
private static bool BackpackToggle;
private static string TorsoName;
private static string ArmName;
private static string LegName;
private static string backpackName;
private static int AccessTorsoHP;
private static int AccessArmHP;
private static int AccessLegHP;
private static int HardpointCount;
private static VALimb.LimbPosition ArmLimbs;
private static VALimb.LimbPosition LegLimbs;
public override void OnInspectorGUI()
{
VALimb _evCtrl = (VALimb)target;
GUILayout.BeginHorizontal();
GUILayout.Label("Torso Limb", GUILayout.Width(70));
TorsoToggle = EditorGUILayout.Toggle("Torso Toggle", TorsoToggle);
GUILayout.EndHorizontal();
if (TorsoToggle == true)
{
ArmToggle = false;
LegToggle = false;
BackpackToggle = false;
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Name", GUILayout.Width(70));
TorsoName = EditorGUILayout.TextField(TorsoName);
_evCtrl.setName(TorsoName);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("HP", GUILayout.Width(70));
AccessTorsoHP = EditorGUILayout.IntField(AccessTorsoHP);
_evCtrl.setHP(AccessTorsoHP);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//GUILayout.Label("Limb Type", GUILayout.Width(70));
VALimb.LimbType BTT = VALimb.LimbType.TORSO;
_evCtrl.LBT = (VALimb.LimbType)EditorGUILayout.EnumPopup("Limb Type", BTT);
_evCtrl.isTorso = true;
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Hardpoints", GUILayout.Width(70));
HardpointCount = EditorGUILayout.IntSlider(HardpointCount, 0, 2);
_evCtrl.HardpointCount = HardpointCount;
GUILayout.EndHorizontal();
}
else if (TorsoToggle == false)
{
TorsoName = "N/A";
AccessTorsoHP = 0;
//HardpointCount = 0;
_evCtrl.LBT = VALimb.LimbType.NONE;
_evCtrl.isTorso = false;
}
GUILayout.BeginHorizontal();
GUILayout.Label("Arm Limb", GUILayout.Width(70));
ArmToggle = EditorGUILayout.Toggle("Arm Toggle", ArmToggle);
GUILayout.EndHorizontal();
if (ArmToggle == true )
{
TorsoToggle = false;
LegToggle = false;
BackpackToggle = false;
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Name", GUILayout.Width(70));
ArmName = EditorGUILayout.TextField(ArmName);
_evCtrl.setName(ArmName);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("HP", GUILayout.Width(70));
AccessArmHP = EditorGUILayout.IntField(AccessArmHP);
_evCtrl.setHP(AccessArmHP);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Accuracy", GUILayout.Width(70));
_evCtrl.vAccuracy = EditorGUILayout.FloatField(_evCtrl.vAccuracy);
//Debug.Log(_evCtrl.vAccuracy);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
ArmLimbs = (VALimb.LimbPosition)EditorGUILayout.EnumPopup("Limb Postion", ArmLimbs);
_evCtrl.ArmPosition = ArmLimbs;
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//GUILayout.Label("Limb Type", GUILayout.Width(70));
VALimb.LimbType BTT = VALimb.LimbType.ARM;
_evCtrl.LBT = (VALimb.LimbType)EditorGUILayout.EnumPopup("Limb Type", BTT);
_evCtrl.isLeg = true;
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Hardpoints", GUILayout.Width(70));
HardpointCount = EditorGUILayout.IntSlider(HardpointCount, 0, 3);
_evCtrl.HardpointCount = HardpointCount;
GUILayout.EndHorizontal();
}
else if(ArmToggle != true)
{
ArmName = "N/A";
AccessArmHP = 0;
//HardpointCount = 0;
ArmLimbs = VALimb.LimbPosition.NONE;
_evCtrl.LBT = VALimb.LimbType.NONE;
_evCtrl.isArm = false;
_evCtrl.vAccuracy = 0.0f;
}
GUILayout.BeginHorizontal();
GUILayout.Label("Leg Limb", GUILayout.Width(70));
LegToggle = EditorGUILayout.Toggle("Leg Toggle", LegToggle);
GUILayout.EndHorizontal();
if (LegToggle == true)
{
TorsoToggle = false;
ArmToggle = false;
BackpackToggle = false;
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Name", GUILayout.Width(70));
LegName = EditorGUILayout.TextField(LegName);
_evCtrl.setName(LegName);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("HP", GUILayout.Width(70));
AccessLegHP = EditorGUILayout.IntField(AccessLegHP);
_evCtrl.setHP(AccessArmHP);
//_evCtrl.LegHP = EditorGUILayout.IntField(_evCtrl.LegHP);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Movement", GUILayout.Width(70));
_evCtrl.vMove = EditorGUILayout.IntField(_evCtrl.vMove);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//GUILayout.Label("Limb Type", GUILayout.Width(70));
LegLimbs = (VALimb.LimbPosition)EditorGUILayout.EnumPopup("Limb Position", LegLimbs);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//GUILayout.Label("Limb Type", GUILayout.Width(70));
VALimb.LimbType BTT = VALimb.LimbType.LEG;
_evCtrl.LBT = (VALimb.LimbType)EditorGUILayout.EnumPopup("Limb Type", BTT);
_evCtrl.isLeg = true;
GUILayout.EndHorizontal();
}
else if (LegToggle != true)
{
LegName = "N/A";
AccessLegHP = 0;
_evCtrl.LBT = VALimb.LimbType.NONE;
_evCtrl.isLeg = false;
_evCtrl.vMove = 0;
}
GUILayout.BeginHorizontal();
GUILayout.Label("Backpack", GUILayout.Width(70));
BackpackToggle = EditorGUILayout.Toggle("Backpack Toggle", BackpackToggle);
GUILayout.EndHorizontal();
if (BackpackToggle == true)
{
TorsoToggle = false;
LegToggle = false;
ArmToggle = false;
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Name", GUILayout.Width(70));
backpackName = EditorGUILayout.TextField(backpackName);
_evCtrl.vIName = backpackName;
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Pack Size", GUILayout.Width(70));
_evCtrl.bSize = EditorGUILayout.IntField(_evCtrl.bSize);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//GUILayout.Label("Limb Type", GUILayout.Width(70));
VALimb.LimbType BTT = VALimb.LimbType.BACKPACK;
_evCtrl.LBT = (VALimb.LimbType)EditorGUILayout.EnumPopup("Limb Type", BTT);
_evCtrl.isBackpack = true;
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
_evCtrl.BackpackType = (VALimb.bType)EditorGUILayout.EnumPopup("Backpack Size", _evCtrl.BackpackType);
GUILayout.EndHorizontal();
}
else if(BackpackToggle != true)
{
backpackName = "N/A";
_evCtrl.LBT = VALimb.LimbType.NONE;
_evCtrl.BackpackType = VALimb.bType.NONE;
_evCtrl.isBackpack = false;
_evCtrl.bSize = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Custom Editor for interfaces 2 Answers
View and Edit Properties of List items in inspector 1 Answer
Can the GameObjectInspector window be customized? 0 Answers
Initialising List array for use in a custom Editor 1 Answer
Is there a way of changing the editor's splash image when Unity boots up? 0 Answers