- Home /
Add trasform to list using editor gui.
I have a set of transform objects with tag = "waypoint" and i have a enemy gameobject.
Now when ever i duplicate a waypoint transform in editor it should automatically add to the enemy script which has a list of transform.
Is this achievable through editor scripting.
I am new to editor scripting and using this code.
public class EnemyTest : MonoBehaviour {
public List<Transform> wayPointList;
}
[CustomEditor(typeof(EnemyTest))]
public class EnemyPathEditor : Editor {
public override void OnInspectorGUI (){
DrawDefaultInspector();
EnemyTest enemy = target as EnemyTest;
GameObject [] wayPoint = GameObject.FindGameObjectsWithTag("Waypoint");
for(int i=0; i<wayPoint.Length; i++){
GameObject wayPointGameObj = EditorGUILayout.ObjectField(wayPoint[i], typeof(GameObject), true) as GameObject;
enemy.wayPointList.Add(wayPointGameObj.transform);
}
}
}
Now whenever i am clicking on enemy the waypoint transforms are getting added continuously.
First i want it to add only the given amount of waypoints which is there in the scene.
Second i want the list to add waypoint automatically whenever i duplicate a gameobject.
Answer by Positive7 · Jul 30, 2015 at 06:52 PM
For the Looping issue :
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class EnemyTest : MonoBehaviour {
[HideInInspector]
public List<Transform> wayPointList = new List<Transform>();
public void Start(){
GameObject[] t = GameObject.FindGameObjectsWithTag("Waypoint");
for (int i = 0; i < t.Length; i++)
{
wayPointList.Add(t[i].transform);
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(EnemyTest))]
public class EnemyPathEditor : Editor {
EnemyTest enemy;
Transform wayPointGameObj;
public void OnEnable(){
enemy = (EnemyTest)target;
}
public override void OnInspectorGUI (){
DrawDefaultInspector();
for (int i = 0; i < enemy.wayPointList.Count; i++)
{
EditorGUILayout.BeginHorizontal();
wayPointGameObj = EditorGUILayout.ObjectField(enemy.wayPointList[i], typeof(Transform), true) as Transform;
if(GUILayout.Button("-")){
enemy.wayPointList.Remove(enemy.wayPointList[i]);
}
EditorGUILayout.EndHorizontal();
/*EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("0")){
enemy.wayPointList[i].position = Vector3.zero;
}
enemy.wayPointList[i].position = EditorGUILayout.Vector3Field("",enemy.wayPointList[i].position);
EditorGUILayout.EndHorizontal();*/
}
}
}
I added a " - " button to remove any unnecessary Transform. I don't think you can check for duplicates, but I'll double check it. If I find something I'll Update the answer.
Your answer
Follow this Question
Related Questions
CustomEditor for a list of polymorphic items 2 Answers
How to prevent Static List from resetting when i run the game in the editor? 1 Answer
How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers
Problem keeping List variables saved when triggering play button 2 Answers
How to correctly modify lists in editor? (changes to lists not changed or corrupted) 0 Answers