Custom Editor - Multi-object editing not supported - c#

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.

Related

Executing a Function Between Script Files

Tell me please. I have 2 scripts that hang on different Unity objects. The first script is for clicking a button. The second one is for executing the function.
How can I execute the AddItem function if the button is pressed?
Script 1 (button click):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
Script 2 (Adding Items):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public void AddItem(ItemScriptableObject _item, int _amount)
{
foreach(InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
return;
}
}
foreach(InventorySlot slot in slots)
{
//print(_item+" "+_amount);
if (slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
return;
}
}
}
If you dont have a variable on the second script, of the first script, you should do that by using:
GameObject go;
go.GetComponent<InventoryManager>();
try changing the first script to something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
void Update()
{
if (isClick)
{
Debug.Log("do something")
}
}
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
This way you can modify the variable: is Click easily.

MainMenu.OnJoinedRoon(): no suitable method found to override Photon Pun

Im writing lobby script, there is a error in title. My code:
Copying scripts from https://youtu.be/EPJIJ_vTXu4?t=750
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
public InputField IFNameRoom;
public void CreateRoom() {
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 4;
PhotonNetwork.CreateRoom(IFNameRoom.text, roomOptions);
}
public void JoinRoon() {
PhotonNetwork.JoinRoon(IFNameRoom.text);
}
public override void OnJoinedRoom() {
PhotonNetwork.LoadLevel("Game");
}
}
For starters there is a typo:
JoinRoom not JoinRoon

How to modify UI text via script?

A simple question: I'm trying to modify UI text (TextMeshPro if that makes any difference) via C# script. I am using the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Coins : MonoBehaviour
{
public Text coins;
void Start()
{
coins = GetComponent<Text>();
}
void Update()
{
coins.text = "text";
}
}
I've done a similar thing in Unity 2018 (I'm currently using Unity 2020.2) and it worked there.
For some reason it doesn't work here though. I would appreciate any help.
Changing text in TMP is practicly the same, but you need to add "using TMPro;" and also change variable type. The code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Coins : MonoBehaviour
{
public TMP_Text coins;
void Start()
{
coins = GetComponent<TextMeshProUGUI>();
}
void Update()
{
coins.text = "text";
}
}
To modify TextMeshPro components, you have to use TMP_Text class.
public class Coins : MonoBehaviour
{
public TMP_Text coins;
void Start()
{
coins = GetComponent<TMP_Text>();
}
void Update()
{
coins.text = "text"; //or coins.SetText(“text”);
}
}
You need to reference the tmp text component instead of the normal Unity text one:
Instead of GetComponent<Text>(); do GetComponent<TextMeshProUGUI>();
Of course don’t forget:
using TMPro;
on top of your code.

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.

Extending Unity UI components with custom Inspector

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.

Categories