Extending UnityEngine.UI.Image and add extra field available in Inspector - unity3d

I am trying to extend UnityEngine.UI.Image like that
public class MyImage : Image {
public string Comment;
}
But I do not see extra text field Comment in inspector. Is it possible to add extra field that would be available in inspector?
PS It triggered as duplicated for Extending Unity UI components with custom Inspector but it is not dupe. I do not ask anything about custom Inspector. It is just regular field with default Inspector. The problem is that field is not appearing in inspector at all.

Unfortunately, the Inspector GUI cannot inherit from base class automatically. You need to write that yourself, just like what is described in Extending Unity UI components with custom Inspector.
MyImage.cs
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class MyImage : Image
{
public string Comment;
}
MyImageEditor.cs
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MyImage))]
public class MyImageEditor : UnityEditor.UI.ImageEditor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();//Draw inspector UI of ImageEditor
MyImage image = (MyImage)target;
image.Comment = EditorGUILayout.TextField("Comment", image.Comment);
}
}
Result:

Related

How to set a button component's transition option to "none" via C# in Unity?

I need to setup my button components in Unity to look like this via C# (i.e. with "Transition" and "Navigation" set to "None").
For the Navigation, it was a little unintuitive but I eventually found that this worked:
Navigation customNav = new Navigation();
customNav.mode = Navigation.Mode.None;
myButton.navigation = customNav;
But I can't for the life of me find a equivalent for "Transition". If it's in the documentation, it must be buried under a heading I can't find. Does anyone have any idea how it can be done?
You can change the transition value by assigning a Selectable.Transition type enum value to the .transition property.
Example
using UnityEngine;
using UnityEngine.UI;
public class TestScript : MonoBehaviour
{
[SerializeField]
private Button target = null;
private void Start()
{
target.transition = Selectable.Transition.None;
}
}
Reference
https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Selectable-transition.html

Auto-Draggable Variables

I want to be able to drag & drop variables (such as floats, integers, maybe even classes) in the Inspector view. So that if I have FirstScript and SecondScript I could easily drag & drop variables between them so that they can change each others' data. I need this to create more modular workflow.
I know that ScriptableObjects allow similar functionality but I need to create them manually through Create button in Inspector. Instead I want this to happen automatically.
This should look something like scripts below. So that I could drag the original_float to float_to_change field right in the Inspector.
// First Component
public class FirstScript : MonoBehaviour
{
public DraggableFloat original_float = 5.0f;
}
// SecondComponent
public class SecondScript: MonoBehaviour
{
public DraggableFloat float_to_change;
public void ChangeFloat()
{
// The value of original_float is changed through reference
float_to_change += 10.0f;
}
}

Trying to customize NGUI - UIButton in the Inspector

When I try to customize a class of "NGUI - UIButton",
I got a problem that's not working serialize variables in Unity editor inspector view.
public class MyButton : UIButton // inherited NGUIButton class
{
public bool _isChecked;
public int _count;
}
I can't see the variables in the inspector of Unity editor.
How can I see the variables in inspector?
I was trying it with [serializeField] script too.
But it's also not working
Is there something special about NGUI?

Unity Editor Can't Edit Multiple Objects

I want to build a custom inspector for one of my classes... and well... I thought I would start simple ... and I still can't get it to draw the basic inspector:
My editor script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(AbilityBluePrint))]
[CanEditMultipleObjects]
public class AbilityBluePrintEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// Show default inspector property editor
DrawDefaultInspector();
}
}
And the class I want to edit is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(fileName = "New Ability BluePrint", menuName = "Ability BluePrint")]
public class AbilityBluePrint : ScriptableObject {
public AbilityName abilityName;
public Characteristic[] characteritics;
public Effect[] effects;
public float coolDown;
public Sprite icon;
public string description;
}
Any suggestions, on how to solve the "multi-object editing not supported" message I get instead of my beautiful custom editor ??
You need to use Serialized properties if like to use Multiple object edition.
[CustomEditor(typeof(AbilityBluePrint))]
[CanEditMultipleObjects]
public class AbilityBluePrintEditor : Editor
{
var AbilityName : SerializedProperty;
function OnEnable ()
{
// Setup the SerializedProperties
AbilityName = serializedObject.FindProperty ("Ability");
}
function OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update ();
...
This is not explained in the documentation and I think it is quite important.
Apart from the option of using Serialized Properties, if you are not able to use those, in case you have your own items, not using the automatically managed Serialized Properties, then you have to use "targets" variable instead of "target".
I noted that if you select different types of objects with no shared script, it doesn't show shared properties, so we don't need to check if the targets are all the same type, it is always one or more. Then you do whatever you want to do by hand with each of them, inside a foreach loop.
Here's a working example of the contents of OnInspectorGUI method inside an Editor class with a simple checkbox that is changed across several scripts. Hope it helps.
var myScript = (UnityTerrainWrapper)target;
var allSelectedScripts = targets;
EditorGUI.BeginChangeCheck();
var value = GUILayout.Toggle(myScript.ShowNativeTerrain, "Draw Unity Terrain");
if (EditorGUI.EndChangeCheck())
{
foreach (var script in allSelectedScripts)
((UnityTerrainWrapper)script).ShowNativeTerrain = value;
SceneView.RepaintAll();
}
DrawDefaultInspector();

Cannot call IVirtualButtonEventHandler Vuforia 5.0.10

Im using Unity 4.7.0 and Vuforia 5.0.10, i cannot call the IVirtualButtonEventHandler.
using UnityEngine;
using System.Collections;
public class VBEventHandler : MonoBehaviour, IVirtualButtonEventHandler
{
}
I just came across this, hope you're still using Unity & Vuforia. You need to add using Vuforia to make the call.
using UnityEngine;
using System.Collections;
using Vuforia;
Register the Virtual Button:
To add a virtual button to an image target, add the VirtualButton element and its attributes to the ImageTarget element in the .xml file.
XML Attributes:
Name - a unique name for the button
Rectangle - defined by the four corners of the rectangle in the
target's coordinate space
Enabled - a boolean indicating whether the button should be enabled
by default
Sensitivity - HIGH, MEDIUM, LOW sensitivity to occlusion
After Registering Virtual Button Code is simple then:
using UnityEngine;
using System.Collections;
using Vuforia;
public class Custom_VirtualButton : MonoBehaviour, IVirtualButtonEventHandler
{
// Use this for initialization
void Start () {
// here it finds any VirtualButton Attached to the ImageTarget and register it's event handler and in the
//OnButtonPressed and OnButtonReleased methods you can handle different buttons Click state
//via "vb.VirtualButtonName" variable and do some really awesome stuff with it.
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
foreach (VirtualButtonBehaviour item in vbs)
{
item.RegisterEventHandler(this);
}
}
// Update is called once per frame
void Update () {
}
#region VirtualButton
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Helllllloooooooooo");
}
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Goooooodbyeeee");
}
#endregion //VirtualButton
}