- Home /
The question is answered, right answer was accepted
Questions Regarding Images in Custom Inspector/Editor
I'm trying to make it so one can edit the texture from the editor right of the name string and left of the Remove Character button, I can get an image to display their if I add it in the Element however I'd like to base.OnInspector and hide those variables later.
I'd like it so in Image 1 you can click the image to change it to a different texture, the way you can in Image 2. If you click them you'll understand what I'm talking about.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System;
public class CutsceneManager : MonoBehaviour
{
public CharacterList c;
void OnEnable()
{
c = GetComponent<CharacterList>();
}
public CharacterList GetCharacterList()
{
return c;
}
[CustomEditor(typeof(CutsceneManager))]
public class Inspector_Buttons : Editor
{
private CharacterList c;
private CutsceneManager cM;
void OnEnable()
{
cM = (CutsceneManager)target;
c = cM.GetCharacterList();
}
public override void OnInspectorGUI()
{
if (GUILayout.Button("Create Character"))
{
AddCharacter();
}
for(int i = 0; i < c.characters.Count; i++)
{
GUILayout.BeginHorizontal();
c.characters[i].name = GUILayout.TextField(c.characters[i].name, GUILayout.Width(100), GUILayout.Height(25));
if (GUILayout.Button("Remove Character"))
{
RemoveCharacter(i);
return;
}
GUILayout.EndHorizontal();
}
base.OnInspectorGUI();
}
void AddCharacter()
{
c.characters.Add(new Character());
}
void RemoveCharacter(int i)
{
c.characters.RemoveAt(i);
}
}
[System.Serializable]
public class CharacterList
{
public List<Character> characters = new List<Character>();
}
[System.Serializable]
public class Character
{
public string name = string.Empty;
public Texture myImage = new Texture();
}
}
[1]: /storage/temp/99940-aosf2.png
Answer by Razputin · Aug 15, 2017 at 06:31 PM
Got it working.
public class CutsceneManager : MonoBehaviour
{
public CharacterList c;
public BackdropList b;
void OnEnable()
{
c = GetComponent<CharacterList>();
b = GetComponent<BackdropList>();
}
public CharacterList GetCharacterList()
{
return c;
}
public BackdropList GetBackdropList()
{
return b;
}
[CustomEditor(typeof(CutsceneManager))]
public class Inspector_Buttons : Editor
{
private CharacterList c;
private BackdropList b;
private CutsceneManager cM;
void OnEnable()
{
cM = (CutsceneManager)target;
c = cM.GetCharacterList();
b = cM.GetBackdropList();
}
public override void OnInspectorGUI()
{
if (GUILayout.Button("Create Character"))
{
AddCharacter();
}
for(int i = 0; i < c.characters.Count; i++)
{
GUILayout.BeginHorizontal();
c.characters[i].name = GUILayout.TextField(c.characters[i].name, GUILayout.Width(100), GUILayout.Height(25));
if (GUILayout.Button("Remove Character"))
{
RemoveCharacter(i);
return;
}
GUILayout.EndHorizontal();
}
if (GUILayout.Button("Create Backdrop"))
{
AddBackdrop();
}
for (int i = 0; i < b.backdrops.Count; i++)
{
GUILayout.BeginHorizontal();
b.backdrops[i].backdropName = GUILayout.TextField(b.backdrops[i].backdropName, GUILayout.Width(100), GUILayout.Height(25));
b.backdrops[i].backdrop = (Texture2D)EditorGUILayout.ObjectField(b.backdrops[i].backdrop, typeof(Texture2D), false);
if (GUILayout.Button("Remove Backdrop"))
{
RemoveBackdrop(i);
return;
}
GUILayout.EndHorizontal();
}
base.OnInspectorGUI();
}
void AddCharacter()
{
c.characters.Add(new Character());
}
void RemoveCharacter(int i)
{
c.characters.RemoveAt(i);
}
void AddBackdrop()
{
b.backdrops.Add(new Backdrop());
}
void RemoveBackdrop(int i)
{
b.backdrops.RemoveAt(i);
}
}
[System.Serializable]
public class CharacterList
{
public List<Character> characters = new List<Character>();
}
[System.Serializable]
public class Character
{
public string name = string.Empty;
public Texture characterPortrait = new Texture();
}
[System.Serializable]
public class BackdropList
{
public List<Backdrop> backdrops = new List<Backdrop>();
}
[System.Serializable]
public class Backdrop
{
public string backdropName = string.Empty;
public Texture2D backdrop;
void OnAwake()
{
backdrop = new Texture2D(10, 10);
}
}
}
Follow this Question
Related Questions
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Custom editor super slow even with no draw operations 1 Answer
Custom inspector, show string instead of component type 0 Answers
Unity Custom Editor Texture Warning 2 Answers
editorguilayout.objectfield issue 1 Answer