Hello,
I need the possibility to add component with a click inside the editor view. In Particular i would like to add a component when the toggle is flagged and remove it when it is unflagged.
I tried different solutions but each of them present the same problem, the toggle is unflagged instantly.
I don’t want to use button because, i should create a button to add and one to remove the component and this for each component i want on the object…
I prefer the toggle because if it is flagged you undestand soon which component is added.
Any help is appreciated, also if you suggest a different way without toggles to do it.
My Solutions:
1st try:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(Initializer))]
public class InitializerEditor : Editor
{
bool component1 = false;
bool component2 = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Initializer script = (Initializer)target;
component1= GUILayout.Toggle(component1, "Component 1");
component2= GUILayout.Toggle(component2, "Component 2");
if (component1)
{
if (script.gameObject.GetComponent<Component1>() == null)
{
script.gameObject.AddComponent<Component1>();
}
}
else
{
Component1 c1= script.gameObject.GetComponent<Component1>();
if (c1 != null)
{
DestroyImmediate(c1);
}
}
if (component2)
{
if (script.gameObject.GetComponent<Component2>() == null)
{
script.gameObject.AddComponent<Component2>();
}
}
else
{
Component2 c2= script.gameObject.GetComponent<Component2>();
if (c2 != null)
{
DestroyImmediate(c2);
}
}
}
}
2nd try
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(Initializer))]
public class InitializerEditor : Editor
{
bool component1 = false;
bool component2 = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Initializer script = (Initializer)target;
component1= GUILayout.Toggle(component1, "Component 1");
component2= GUILayout.Toggle(component2, "Component 2");
if (component1)
{
if (script.gameObject.GetComponent<Component1>() == null)
{
script.gameObject.AddComponent<Component1>();
}
component1 = true;
}
else
{
Component1 c1= script.gameObject.GetComponent<Component1>();
if (c1 != null)
{
DestroyImmediate(c1);
}
}
if (component2)
{
if (script.gameObject.GetComponent<Component2>() == null)
{
script.gameObject.AddComponent<Component2>();
}
component2 = true;
}
else
{
Component2 c2= script.gameObject.GetComponent<Component2>();
if (c2 != null)
{
DestroyImmediate(c2);
}
}
}
}
3rd try
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(Initializer))]
public class InitializerEditor : Editor
{
bool component1 = false;
bool component2 = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Initializer script = (Initializer)target;
component1= GUILayout.Toggle(component1, "Component 1");
component2= GUILayout.Toggle(component2, "Component 2");
if (component1)
{
script.addComponent("Component1"); //i made all the checks into the initializer script
}
else
{
script.removeComponent("Component1");
}
if (component2)
{
script.addComponent("Component2");
}
else
{
script.removeComponent("Component2");
}
}
}
If i try the following, all works perfectly and the flag remains falgged and i can unflag whenever i want.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(Initializer))]
public class InitializerEditor : Editor
{
bool component1 = false;
bool component2 = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Initializer script = (Initializer)target;
component1= GUILayout.Toggle(component1, "Component 1");
component2= GUILayout.Toggle(component2, "Component 2");
if (component1)
{
Debug.Log("Component1 added!");
}
else
{
Debug.Log("Component1 removed!");
}
if (component1)
{
Debug.Log("Component2 added!");
}
else
{
Debug.Log("Component2 removed!");
}
}
}