I'd like to add documentation to my custom Components. This documentation should be visible in the editor.
It seems already available for standard library components :
However, it doesn't work for my code:
using UnityEngine;
using System.Collections;
public class MainPlayer : MonoBehaviour {
// I would like to see this comment in the editor too!
public string myName;
// Use this for initialization
void Start () {
Debug.Log("I am alive and my name is " + myName);
}
// Update is called once per frame
void Update () {
}
}
How can I do that?
All you need to do is decorate the field with Tooltip attribute.
Example:
[Tooltip("These are not droids you are looking for...")]
public string myName;
Also, if I were you, I'd go [Header()] attribute way as it will always show a bolded header above field in Inspector instead of only showing information on hover.
Docs for Header and other attributes:
http://docs.unity3d.com/ScriptReference/HeaderAttribute.html
I'm afraid there is no direct link to the whole section, so you need to navigate in nav on the left to section called Attributes which should be active from start.
Related
I was wondering if anyone knew how to make nested variables inside the unity inspector with a script, kind of like this:
Doing so does require knowledge of UnityEditor and not only (as you say ... nested variables) can give you many other control options in the inspector. To do this, I created a sample code called MenuManager. As you can see this code:
public class MenuManager : MonoBehaviour
{
public bool variable1;
public float nestedVariable;
//...
}
Unity itself does not provide any attributes such as [Range] or [Header] for such a request, and to do this you need to define a CustomEditor for the class but Before to do that, you need to create a folder similar to the photo with name of Editor and put it in the Assets folder. Then create another script with name of MenuEditor (for example here ..) and put it in a Editor folder.
Now open the MenuEditor code. Inherit it from the Editor class. Editor class is base class for editing inspector and more. It will give you many override methods with access to the features inside the unity editor. and make sure it has two Attributes Custom Editor as well as CanEditMultipleObjects.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
//..
}
This code gives you access to the MenuManager script. According to the following code, I coded a Nested variable to the first one.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
public override void OnInspectorGUI()
{
var myMenu = target as MenuManager; // target is script reference that we want to manipulate it
myMenu.variable1 = EditorGUILayout.Toggle("Variable 1", myMenu.variable1); // show first variable on inspector
GUI.enabled = myMenu.variable1; // access to second variable depend of first
myMenu.nestedVariable =EditorGUILayout.Toggle("Nested Variable", myMenu.nestedVariable);
GUI.enabled = true;
}
}
After finishing the work, you can access the nested variable only by setting first one to true.
Remember after doing this you can access many other features just inside MenuEditor class but if you find this difficult, I suggest you use Odin Inspecter. I hope you have reached your answer. comment under answer if you need more information.
Context : I'm editing a prefab, I have two components on it, both are custom scripts.
When editing a prefab, I want to remove the component Container Sync at the same time that I remove the component Container Descriptor in the editor by right clicking it and clicking on Remove Component. See the picture below.
In ContainerDescriptor, I have a reference towards the ContainerSync script.
public class ContainerDescriptor : MonoBehaviour
{
public ContainerSync containerSync;
}
I tried to use the method OnDestroy() of the ContainerDescriptor script, but it's not called when removing ContainerDescriptor that way.
On this thread there is a solution at least for if you remove that component via the context menu like you do.
Using [ExecuteAlways] the event message calls that usually are only called in PlayMode do also get called in Edit and in Prefab mode!
You can do something like e.g.
[ExecuteAlways]
public class ContainerDescriptor : MonoBehaviour
{
public ContainerSync containerSync;
#if UNITY_EDITOR
private void OnDestroy ()
{
if(containerSync)
{
if(Application.isPlaying)
Destroy(containerSync);
else
DestroyImmediate (containerSync);
}
}
#endif
}
Note though that specifically for prefabs this might still fail since afaik neither Destroy nor DestroyImmediate can be used inside of prefabs (see here)
How do I set the cursor style for a VisualElement to one of the predefined classes in code? (Arrow, Text, Split Resize Left Down, etc)
I am aware the cursor can be set using the CursorStyle property
style.cursor = SomeCursorStyle;
The problem is I want to use the SplitResizeLeftRight cursor and I can't seem to find where the static classes are defined. Obviously I can do this though a stylesheet, but I want to do it through code. An alternative is to create my own CursorStyle and point it to the appropriate cursor textuer, but again, I can't seem to find where those are located.
According to these docs and the UIElement Debugger I know I can change the style of the cursors. I just can't figure out how to set it to one of those in code.
The UIElementsEditorUtility class no longer exists. I found a solution for the more recent version of Unity (2022.1.7 In my case) but it includes some C# Reflection...
//using System.Reflection;
public static void SetCursor(this VisualElement element, MouseCursor cursor)
{
object objCursor = new Cursor();
PropertyInfo fields = typeof(Cursor).GetProperty("defaultCursorId", BindingFlags.NonPublic | BindingFlags.Instance);
fields.SetValue(objCursor, (int)cursor);
element.style.cursor = new StyleCursor((Cursor)objCursor);
}
I hope it will help those who have the same problem
Take a look at the MouceCursor enum:
https://docs.unity3d.com/ScriptReference/MouseCursor.html
"Custom mouse cursor shapes used with EditorGUIUtility.AddCursorRect."
It defines multiple cursor shapes, one of them is SplitResizeLeftRight. Usage example:
EditorGUIUtility.AddCursorRect(new Rect(20, 20, 140, 40), MouseCursor.SplitResizeLeftRight);
I hope this helps you.
By some miracle I stumbled upon this class: UIElementsEditorUtility
At the time of posting the class can not be found in Unity docs.
So here is an example of how to use it:
style.cursor = UIElementsEditorUtility.CreateDefaultCursorStyle(MouseCursor.ResizeHorizontal);
I recently went into creating my own personal DSL with xtext and manage to create a mini programming language based on C (simple expressions and basic functions). My current task it to create a custom tree view for the language that would allow me to see all the functions as root elements and the instructions inside them as children.
My actual problem that I can't seem to resolve is exactly how do I make the custom tree view I wish to create take the data from the file that I'm currently working on.
I have an RCP product ready for the DSL that I can use and I would like to include this view over there.
I have created the interface for the view with WindowBuilder and made it as a ViewPart.
In the end I wish it to look close to what the standard outline for a java program looks like.
Thanks for the help in advance.
If you work with your own view, you can add an IPartListener implementation that will notify you on when an editor is activated with the following code:
getViewSite().getPage().addPartListener(new IPartListener() {
#Override
public void partOpened(IWorkbenchPart part) {
}
#Override
public void partDeactivated(IWorkbenchPart part) {
}
#Override
public void partClosed(IWorkbenchPart part) {
}
#Override
public void partBroughtToTop(IWorkbenchPart part) {
}
#Override
public void partActivated(IWorkbenchPart part) {
// Add view initialization from the new part
}
});
I am struggling to make an header of content panel look like blinking by changing the color of it. But the code I am trying does not work, here the code:
public class Reminder extends ContentPanel{
Timer time = new Timer{
public void run(){
rpc.getReminders(new AsyncCallBack<ArrayList<ModelData>>(){
public voidonFailure(Throwable caught)
{ }
public void onSuccess(ArrayList<ModelData> result){
//the next line does not affect any result
getHeader().setStyleAttribute("backgroundColor","red");
//even tried throught css, but both of them gave no result
getHeader().addStyleName("myredpanel");
//But this method is working, but its also overwriting the parents css styles, but i only need to change backgroun color
getHeader().setStyleName("myredpanel");
})
Searched the forums, but those examples doesnot working on mine? What else suggestions?
Don't use the camel case names when you call setStyleAttribute.
getHeader().setStyleAttribute("backgroundColor","red"); // WRONG
getHeader().setStyleAttribute("background-color","red"); // CORRECT
if you are using gxt 3.0.X you should change ContentPanelAppereance, direct style setting may not be helpfull. if you want, i can send an example.