Extending Unity UI components with custom Inspector - c#

Is it possible to extend the new unity ui components like for example the transform component? Because nothing happens when i try to extend the button, instead of the transform component
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Transform))]
public class CustomTransform : Editor
{
public override void OnInspectorGUI()
{
}
}

Yes you can extend UI components and write them their own custom inspector.
You just need to remember to use right namespaces and also inherit from the right Inspector class.
You can of course override too!.
Example here is a UISegmentedControlButton
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class UISegmentedControlButton : Button {
public Sprite offSprite;
public Sprite onSprite;
public Color offTextColor = Color.white;
public Color onTextColor = Color.white;
private bool isSelected;
public bool IsSelected {
get {
return isSelected;
}
set {
isSelected = value;
Text text = this.transform.GetComponentInChildren<Text>();
if (value) {
this.GetComponent<Image>().sprite = onSprite;
text.color = onTextColor;
} else {
this.GetComponent<Image>().sprite = offSprite;
text.color = offTextColor;
}
}
}
public override void OnPointerClick(PointerEventData eventData) {
this.transform.parent.GetComponent<UISegmentedControl>().SelectSegment(this);
base.OnPointerClick(eventData);
}
}
And Editor class for it:
P.S. ButtonEditor is different from UnityEditor.UI.ButtonEditor as the first one is from UnityEngine.ButtonEditor. To access .UI from UnityEditor, you need to put your Editor script under Editor folder
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Collections;
[CustomEditor(typeof(UISegmentedControlButton))]
public class UISegmentedControlButtonEditor : UnityEditor.UI.ButtonEditor {
public override void OnInspectorGUI() {
UISegmentedControlButton component = (UISegmentedControlButton)target;
base.OnInspectorGUI();
component.onSprite = (Sprite)EditorGUILayout.ObjectField("On Sprite", component.onSprite, typeof(Sprite), true);
component.onTextColor = EditorGUILayout.ColorField("On text colour", component.onTextColor);
component.offSprite = (Sprite)EditorGUILayout.ObjectField("Off Sprite", component.offSprite, typeof(Sprite), true);
component.offTextColor = EditorGUILayout.ColorField("Off text colour", component.offTextColor);
}
}
Also here is a useful link directly to the source of Unity UI
https://bitbucket.org/Unity-Technologies/ui/src
And a little proof that it works:

Tried the example above but EditorGUILayout.ObjectField gets empty when I exit the prefab I placed my object in.
However, this method works without zeroing the field.
using Game.Ui.Views.DialoguePanel;
using UnityEditor;
using UnityEditor.UI;
namespace Editor
{
[CustomEditor(typeof(MyButtonExtension))]
public class MyButtonExtensionDrawer : ButtonEditor
{
SerializedProperty m_testObject;
protected override void OnEnable()
{
base.OnEnable();
m_testObject = serializedObject.FindProperty("testObject");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(m_testObject);
serializedObject.ApplyModifiedProperties();
}
}
}
In fact, this is just a slightly modified content of the ButtonEditor class.

Related

how do I set a color to a spefic/certain buttons in unity

I have a ButtonManger.cs file which takes an array of buttons. I want to know how I can change all the buttons colors by the empty object(My ButtonManger Object). so basically once a button(that is in the array) is trigged/clicked it will tell the buttonmanger to change the color of the buttons(in array).
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ButtonManger : MonoBehaviour
{
public Button[] button;
public string selectedButton { get; private set; }
private void Awake()
{
button = FindObjectsOfType<Button>();
selectedButton = EventSystem.current.currentSelectedGameObject.name;
}
public void OnClickedButton()
{
GetComponents<Button>().material.color = new Color(0.3f, 0.4f, 0.6f, 0.3f);
//this is where I cant get it to work, getComponents<Button>().material doesnt work
}
}```
This does the job for me:
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;
public class ChangeButtonColor : MonoBehaviour
{
Button[] buttonsArray;
void Awake()
{
buttonsArray = FindObjectsOfType<Button>();
}
public void ChangeButtonColors()
{
var newColor = Color.blue;
for (int i = 0; i < buttonsArray.Length; i++)
{
buttonsArray[i].GetComponent<Image>().color = newColor;
}
}
}
Change the color to your preferences on the first line of code under the ChangeButtonColor method.
Go to your buttons that and attach this method to whatever buttons you want.
Let me know if you have any other questions.
Vlad
Material color can be set with:
https://docs.unity3d.com/ScriptReference/Material.SetColor.html
Keep in mind that your material can have multiple colors based on the used shader. Default is _Color. I also recommend setting Color to public space so you can easily adjust it in the Inspector.
public Color color_buttonpressed;
public void OnClickedButton()
{
GetComponents<Button>().material.SetColor("_Color",color_buttonpressed);
}

Unity3d:Add a public script via inspector

So I am creating a game, which is a Novel game. I want to add a functionality where my friend can add a new dialogue on a character game object. To do that, I already created a main script that enqueue and dequeue all of what is written on the inspector. Two more scripts, one script is a class for creating a new properties on the inspector where my friend can write, the other script functionality is to patch it on the inspector itself. I decided to add a patcher to customize unity editor to add a button. All what is left is a function to add another class where in my friend can write another character name and sentences.
This is what it looks like on Unity Inspector:
Please Help.
DialogueManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
private Queue<string> sentences;
public Text WrapperName;
public Text WrapperContent;
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
WrapperName.text = dialogue.name;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
WrapperContent.text = sentence;
}
public void EndDialogue()
{
Debug.Log("dialogue Ended..");
}
}
Dialogue.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}
StoryElement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoryElement : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
elementscriptpatcher.cs
using System.Collections;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(StoryElement))]
public class elementscriptpatcher : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(GUILayout.Button("Add another script"))
{
// I need to write a function for appending a class Dialogue on Dialogue.cs which was initialized on the StoryElement.
}
}
}
you can use GameObject.AddComponent.
if(GUILayout.Button("Add another script"))
{
gameObject.AddComponent<Dialogue>();
}
EDIT : As #derHugo said in comments, we can't add a class that's base is not Monobehaviour. So you will require an Access Class to access Dialogue.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AccessClass : MonoBehaviour {
public Dialogue mClassObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Use Ink for Unity, and author the dialogue as text files.

Unity text not updating

I'm trying to get the title/description of a playing card to change by creating a card class that holds the information:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (menuName = "Card")]
public class Card : ScriptableObject
{
public string cardName;
public Sprite art;
public string cardDetail;
}
Then load it with another script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CardViz : MonoBehaviour
{
public Text title;
public Text detail;
public Image art;
public Card card;
private void start()
{
LoadCard(card);
}
public void LoadCard(Card c)
{
if (c == null)
{
return;
}
card = c;
title.text = c.cardName;
detail.text = c.cardDetail;
art.sprite = c.art;
}
}
I created prefab with the basic layout of a card. Then I created a new asset value in unity for a card and given it a name and detail. Then assigned it to the public valuable Card under CardViz along with the corresponding title, detail and image variable to create a new prefab but none of the text change when I drag the newly made prefab into the hierarchy. Any clue as to what I'm doing wrong here?
A small typo. Your start method needs to have a capital s.
private void Start()
{
LoadCard(card);
}

How can I create a custom inspector guilayout.toggle?

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Control))]
public class ControlEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Control control = (Control)target;
if (GUILayout.Toggle(control.isControl, "Control"))
{
control.ToControl();
}
}
}
And
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control : MonoBehaviour
{
public Rigidbody rigidbody;
public bool isControl = false;
// Start is called before the first frame update
void Start()
{
}
public void ToControl()
{
if(isControl == false)
{
}
else
{
Destroy(rigidbody);
}
}
}
What I want to do is a guilayout.toggle or a button and to be able to destroy and to add a Rigidbody to the gameobject the Control script will be on.
How do I create back add the Rigidbody to the gameobject ?
And how do I use the isControl flag ? The idea is to use the guilayout.toggle in the editor script.
I want to destroy or add a new rigidbody while the game is running ! But using a guilayout.toggle or button in the inspector.
Actually you wouldn't need an inspector script for that at all. Simply add a repeadetly check for the bool like e.g. in LateUpdate and make the component [ExecuteInEditoMode] like
using UnityEngine;
[ExecuteInEditoMode]
public class Control : MonoBehaviour
{
public Rigidbody rigidbody;
public bool isControl;
// repeatedly check the bool
private void LateUpdate()
{
ToControl();
}
public void ToControl()
{
if (!isControl && rigidbody)
{
// in editmode use DestroyImmediate
if (Application.isEditor && !Application.isPlaying)
{
DestroyImmediate(rigidbody);
}
else
{
Destroy(rigidbody);
}
rigidbody = null;
}
else if(isControl && !rigidbody)
{
rigidbody = gameObject.AddComponent<Rigidbody>();
// adjust settings of rigidbody
}
}
}
This way LateUpdate is called both, in playmode and in editmode, and will simply react to the isControl value.
Ofcourse there is an overhead for calling this LateUpdate all the time so if you want to avoid it you can call it only from the editor. However, since you are using base.OnInspectorGUI(); you don't really need an additional Toggle since you already have the one of the default inspector.
So could simply do
using UnityEngine;
public class Control : MonoBehaviour
{
public Rigidbody rigidbody;
public bool isControl;
public void ToControl()
{
if (!isControl && rigidbody)
{
if (Application.isEditor && !Application.isPlaying)
{
DestroyImmediate(rigidbody);
}
else
{
Destroy(rigidbody);
}
rigidbody = null;
}
else if(isControl && !rigidbody)
{
rigidbody = gameObject.AddComponent<Rigidbody>();
}
}
}
and in the editor script simply do
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Control))]
public class ControlEditor : Editor
{
private Control control;
// calle when the object gains focus
private void OnEnable()
{
control = (Control)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (!control.isControl && control.rigidbody)
{
control.ToControl();
Repaint();
}
else if (control.isControl && !control.rigidbody)
{
control.ToControl();
Repaint();
}
}
}
BUT you will already notice that this might affect how Undo/Redo works - in this case it would e.g. reset the isControl value but not remove along the RigidBody component leading to errors (see more below)
Or since you asked it you can add the ToggleField (currently you will have it twice since one also ships with base.OnInspectorGUI();)
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Control))]
public class ControlEditor : Editor
{
private Control control;
// calle when the object gains focus
private void OnEnable()
{
control = (Control)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
control.isControl = EditorGUILayout.Toggle("additional isControl", control.isControl);
if (!control.isControl && control.rigidbody)
{
control.ToControl();
Repaint();
}
else if (control.isControl && !control.rigidbody)
{
control.ToControl();
Repaint();
}
}
}
BUT you will notice that this solution changing the value using the additional isControl lacks the possibility of using Undo/Redo completely and it will NOT mark your scene as "dirty" so Unity might not save those changes!
So if you really want to have your custom toggle field in an inspector script I would actually recommend strongly to use proper SerializedPropertys instead of directly making changes to the target (sometimes it can't be avoided like with the adding of the component though):
[CustomEditor(typeof(Control))]
public class ControlEditor : Editor
{
private SerializedProperty _isControl;
private SerializedProperty rigidbody;
private Control control;
// calle when the object gains focus
private void OnEnable()
{
control = (Control)target;
// link serialized property
_isControl = serializedObject.FindProperty("isControl");
rigidbody = serializedObject.FindProperty("rigidbody");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// load current values into the serialized copy
serializedObject.Update();
if (!_isControl.boolValue && rigidbody.objectReferenceValue)
{
DestroyImmediate(rigidbody.objectReferenceValue);
rigidbody.objectReferenceValue = null;
}
else if (_isControl.boolValue && !rigidbody.objectReferenceValue)
{
var rb = control.gameObject.AddComponent<Rigidbody>();
rigidbody.objectReferenceValue = rb;
}
// write back changed serialized values to the actual values
serializedObject.ApplyModifiedProperties();
}
}
This looks more complicated and actually you have duplicate code but it gives you full Undo/Redo support and marks your objects and scenes dirty so Unity saves the changes.

Custom Editor - Multi-object editing not supported

I have had no previous issues creating custom editors, however with this one it seems to be informing me "Multi-object editing not supported" the code for my script is below as well as the script for my custom editor. Is there something that I'm missing?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Biophase Games/UI/Togglr")]
[System.Serializable]
public class Togglr : MonoBehaviour {
public List<Toggler> toggles;
public ColorBlock onColors;
public ColorBlock offColors;
public string value;
void Update() {
foreach(Toggler t in toggles) {
if (t.toggle.isOn == true) {
value = t.text;
t.toggle.colors = onColors;
} else {
t.toggle.colors = offColors;
}
}
}
}
[System.Serializable]
public class Toggler {
public Toggle toggle;
public string text;
}
and the CustomEditor script
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(Togglr))]
[CanEditMultipleObjects]
public class TogglrEditor : Editor {
SerializedProperty onColors;
SerializedProperty offColors;
SerializedProperty toggles;
void OnEnable() {
onColors = serializedObject.FindProperty ("onColors");
offColors = serializedObject.FindProperty ("offColors");
toggles = serializedObject.FindProperty ("toggles");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField (toggles, true);
EditorGUILayout.PropertyField (onColors);
EditorGUILayout.PropertyField (offColors);
serializedObject.ApplyModifiedProperties ();
}
}
On an attempt to solve my issue, I moved the class Toggler into it's own file which subsequently solved the issue I was having.

Categories