- Home /
Editor Window Edit property of items in a list
Hello, need a bit of help I have a script that serve as manager for items in my game I'm trying to make a custom editor window to display in a list only the item with that manager script. And I want to be able to edit the properties of that script on each item, from that window.
I thought ReorderableList would be the solution, but after hours trying I'm getting nowhere and I don't understand half of what I'm doing, following scattered tutorials left and right. Ideally I want this http://va.lent.in/content/images/2014/Jun/list_2.png But I've no idea how to get to this
This is my Manager script
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public enum CubeGroups {Red, Boing, Black, Green, Roll, Spawner}
public class CubeManager : MonoBehaviour {
public bool startCube;
public CubeGroups myGroup;
[Header("Smaller Number means longer life")]
public float lifeTime;
static public float _chrono;
void Start(){
_chrono = 25;
}
void Update(){
_chrono -= Time.deltaTime;
if (_chrono < 0) {
transform.tag = "Cube";
}
if (!startCube) {
float _a = GetComponent<MeshRenderer> ().material.color.a;
_a -= (lifeTime * Time.deltaTime);
GetComponent<MeshRenderer> ().material.color = new Color (GetComponent<MeshRenderer> ().material.color.r, GetComponent<MeshRenderer> ().material.color.g, GetComponent<MeshRenderer> ().material.color.b, _a);
if (_a <= 0) {
Destroy (this.gameObject);
}
}
}
}
And this is my attempt at editor window
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
public class CubeWindow : EditorWindow {
CubeManager[] cubemag = (CubeManager[])FindObjectsOfType (typeof(CubeManager));
[MenuItem ("Window/Cube Window")]
public static void ShowWindow () {
EditorWindow.GetWindow(typeof(CubeWindow));
}
void OnGUI() {
GUILayout.Label ("List of Cubes", EditorStyles.boldLabel);
cubemag = (CubeManager[])FindObjectsOfType (typeof(CubeManager));
for(int i = 0; i < cubemag.Length; i++)
{
cubemag[i] = (CubeManager)EditorGUILayout.ObjectField(cubemag[i], typeof(CubeManager));
}
}
}
If anyone know how to do so, and can explain me with a simple step-by-step because I've never fiddled with Unity gui before so I'm completely lost...
Answer by RobAnthem · Jan 04, 2017 at 08:29 PM
Well I got it to display for you, the rest is up to you :P
CubeWindow
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class CubeWindow : EditorWindow //Deriving from editor window to get window and GUI elements.
{
private CubeManager[] Cubemag; //Obiously your array of target objects
[MenuItem("Window/Cube Window")] //Declaring to Unity engine that this is meant as a menu item.
public static void ShowWindow() //Telling unity what MenuItem - "WIndowCube" does.
{
EditorWindow cubeWindow = EditorWindow.GetWindow(typeof(CubeWindow)); //Getting our window for unity to use.
GUIContent titlecontent = new GUIContent("Cube Window"); //Creating some title content.
cubeWindow.titleContent = titlecontent; //Setting the title content to the window.
cubeWindow.maxSize = new Vector2(300, 500); //Declaring the max size of the window.
cubeWindow.minSize = new Vector2(300, 500); //Declaring the min size of the window.
cubeWindow.ShowPopup(); //Showing our window as a Popup, I.E., a default editor style.
}
void OnGUI() //OnGUI gets called like an Update... very very often, avoid making large data collection calls here.
{
EditorGUILayout.BeginVertical(EditorStyles.helpBox); //Declaring our first part of our layout, and adding a bit of flare with EditorStyles.
GUILayout.Label("List of Cubes", EditorStyles.boldLabel); //Making a label in our vertical view, declaring its contents, and adding editor flare.
Cubemag = FindObjectsOfType<CubeManager>(); //Collecting our cube info, I suggest finding a way to do this outside of OnGUI.
for (int i = 0; i < Cubemag.Length; i++) //Your loop to display the objects, THIS must exist in OnGUI.
{
if (Cubemag[i] != null) //Preventing null errors if the objects are removed.
{
Cubemag[i] = (CubeManager)EditorGUILayout.ObjectField(Cubemag[i], typeof(CubeManager), true); //Declaring our object as an object field, with the type of object we want, and allowing scene object.
EditorGUILayout.BeginHorizontal(); //Adding a horizontal view to indent our next line so that the properties look like children, LOTS of things you can add like toggles and stuff to reduce this clutter.
GUILayout.Space(15); //The indent for our next line, in pixels.
Cubemag[i].lifeTime = EditorGUILayout.FloatField("Life Time", Cubemag[i].lifeTime); //the lifeTime property of cubemap can be assigned to a GUI element here.
EditorGUILayout.EndHorizontal(); //All layout areas must be closed like this.
}
}
EditorGUILayout.EndVertical(); // And closing our last area.
}
}
It work! Thank you so much, I should indeed be able to do the rest on my own! Also thanks for all the commenting, it's much more clear now :D
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Instantiate to temp var or directly adding to list? 1 Answer
Creating item-packages with properties 2 Answers
Combining Lists 1 Answer