#↕️┃editor-extensions

1 messages · Page 117 of 1

visual stag
#

All the Google results say to remove and add the component again. Not sure what the deal is

abstract solar
#

Like OnDestroy but for the editor instead of runtime

gloomy chasm
abstract solar
#

Also I do need to know what gameobject was destroyed, so having an ondestroy inside it would be helpful

visual island
#

I remember seeing somewhere that a new feature was added in one of the latest versions of the editor, allowing drawing expandable SO inspector within another object inspector that references the said SO. Could someone point me in the right direction? Because google doesn't seem to give any related results...

#

Kinda like you can expand a serializable c# class/struct

#

Is it just my imagination? Was there no such feature added?🤔

visual island
#

Basically drawing the SO properties in another object inspector(that references the so)

gloomy chasm
#

Oh, I see you mean show the editor of one SO in the editor of another unity object?

#

No, nothing was added for that. But you can already do that as is.

visual island
#

Okay, so how would I do that?

#

Is there a ready to use attribute that I can just add to my SO field? Or do I have to write editor scripts?

gloomy chasm
#

You have to do custom editor stuff. If you are using IMGUI, then you use Editor.CreateEditor.
If you are using UITK then you use InspectorElement

visual island
#

And here I thought I can just do a quick hack without spending time on learning that stuff. 😄 Oh well.

#

Thanks for pointing me in the right direction though

burnt dove
#

How can I avoid StackOverflowException in this code?

public class MyDrawer : PropertyDrawer
{
    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        PropertyField propertyField = new PropertyField(property); // <- Causes StackOverflowException.
        DoSomething(propertyField);
        return propertyField;
    }
}

I don't know how to get the default property field of the property in order to apply my custom modifications on it.

slim zinc
#

that code should not cause a stackoverflow

burnt dove
# slim zinc that code should not cause a stackoverflow

This causes stackoverflow in my editor.

[CustomPropertyDrawer(typeof(MyAttribute), true)]
public class MyDrawer : PropertyDrawer
{
    public override VisualElement CreatePropertyGUI(SerializedProperty property) => new PropertyField(property);
}
public class MyAttribute : PropertyAttribute { }
public class MyMonoBehaviour : MonoBehaviour
{
    [MyAttribute]
    public int MyField;
}
[CustomEditor(typeof(MyMonoBehaviour))]
public class MyEditor : Editor
{
    public override VisualElement CreateInspectorGUI() => new PropertyField(serializedObject.FindProperty(nameof(MyMonoBehaviour.MyField)));
}

Doesn't in yours?

slim zinc
#

i don't see why it should

burnt dove
# slim zinc i don't see why it should

new PropertyField(property) internal request to Unity all the custom drawers in order to get the draw the specific field. One of those custom drawers (MyDrawer) calls new PropertyField(property). So it is recursive.

whole steppe
#

Hello, I'm trying to change prefabs by the bulk but the PrefabUtility API doens't seem to be working, can someone help me with this?

    var childComponents = prefabLoaded.GetComponentsInChildren<ChildComponent>();
     
    foreach (var comp in components)
    {
        comp.ListInsideChildComponent.Clear();
    }
     
    PrefabUtility.SavePrefabAsAsset(prefab, prefabPath);
    PrefabUtility.UnloadPrefabContents(prefab)

This doesn't seem to properly be changing the prefabs in this case.

gloomy chasm
tulip panther
#

I still am a dumb and can't get Unity to display the extra stuff in VS

burnt dove
slim zinc
#

yeah. that's why i was wondering. i couldn't reproduce this

whole steppe
tame onyx
#

How do I track changes for Undo when the target object does not derive from unity's Object class?

#

I want to undo a position change but the object is my own class

gloomy chasm
gloomy chasm
tame onyx
tame onyx
#

I am rendering a sphere and handles to that point

gloomy chasm
#

Or does it reset?

tame onyx
gloomy chasm
tame onyx
gloomy chasm
#

So they are fields on a UnityEngine.Object 😛

tame onyx
gloomy chasm
tame onyx
tame onyx
tame onyx
#

structure is the gameobject

gloomy chasm
#

You sure? Last I checked GameObject doesn't have a connections property.

tame onyx
tame onyx
gloomy chasm
#

Really I would just use SerializedObject and SerializedProperty instead so it will automatically support Undo and prefab overrides.

gloomy chasm
tame onyx
gloomy chasm
# tame onyx how would I do that?
using var serializedObject = new SerializedObject(structure);
SerializedProperty connectionsProperty = serializedObject.FindProperty("connections");

foreach(SerializedProperty c in connectionsProperty)
{
  SerializedProperty positionProperty = c.FindRelativeProperty("position");

  positionProperty.vector3Value = TransformHandles(positionProperty.vector3Value, default);
}
serializedObject.ApplyModifiedProperties();
#

Probably want to the SerializedObject instead of creating and destroying it though.

tame onyx
gloomy chasm
#

You should be able to start typing and get suggestions

tame onyx
gloomy chasm
tame onyx
#

also whats the "using" for?

gloomy chasm
gloomy chasm
tame onyx
gloomy chasm
tame onyx
tame onyx
gloomy chasm
tame onyx
gloomy chasm
tame onyx
tame onyx
gloomy chasm
tame onyx
gloomy chasm
wintry trout
#

How do I create an EditorWindow (for which I already have a ready script with ShowWindow whish is supposed to be called to show the window) from another script? I'm asking because EditorWindow is an SO and it's not recommended to create them with new and also making the method ShowWindow static would mean I can only use static variables inside the method and static variables can't be saved into the script meta via Inspector

#

It's like I have a button in an Editor and what to show an EditorWindow once the button is pressed

limpid linden
#

Static methods can be called anywhere, not just in other static methods/classes

wintry trout
whole steppe
# gloomy chasm Try using this https://docs.unity3d.com/ScriptReference/PrefabUtility.EditPrefab...

Sadly that isn't working, I tried doing:

                    {
                        var prefabLoadedContents = editingScope.prefabContentsRoot;
                        var components = prefabLoadedContents.GetComponentsInChildren<CustomComponent>();
     
                        foreach (var comp in components)
                        {
                            Debug.Log($"{comp.name} {comp.ArrayInComponent.Count}");
                            comp.ArrayInComponent.Clear();
                            Debug.Log($"{comp.name} {comp.ArrayInComponent.Count}");
                        }
                    }

And the fields in the child of the prefab don't change. However if I make any changes to root it works. The count print works perfectly, it shows it's filled before and that it was changed, but nothing applies.

limpid linden
wintry trout
#

In static ShowWindow

limpid linden
#

@wintry trout
You can get the reference to the window that is being shown and set fields:

public static void ShowWindow()
{
  CustomWindow window = EditorWindow.GetWindow<CustomWindow>();
  window.foo = "bar";
}
wintry trout
#

So I can't use anything serialized this way

limpid linden
#

where are you trying to access data

#

if it's not from the window

#

You could make a method that the button calls and handle non-static fields in that method?

wintry trout
#

Sure I can

wintry trout
#

I have an Image with RenderTexture inside it in my EditorWindow but seems like the EditorWindow (and specifically the RenderTexture inside the Image) gets update only when cursor is over the EditorWindow, is there a way to fix it?

dim slate
#

Requireconstantrepaint I believe is a thing now, you can also just call Repaint() after changes are made

wintry trout
#

can't find it in net

dim slate
#

If I remember correctly it's either an attribute or a method, one sec

#

Phones about to die, just call Repaint() at the end of your gui method and it should work

wintry trout
#

ty

dim slate
#

Hmmm send the code, you may be updating the texture in a weird way, @limpid linden is usually pretty good at finding that stuff

wintry trout
#

oh wait...

#

it works

#

ty

final pond
#

I got annoyed with the coloring system in unity so i made my own fix. If anyone wants it then dm me but i just came because i was bored

#

but ill also put it here

slim zinc
# final pond

why did you package it as a unitypackage if it is literally 1 script

final pond
#

true that

wintry trout
#

Making an Editor button which changes value of a Vector3 field existing inside of the inspected class. But my changes do not get applied for some reason. Any ideas why? (code block from the Editor class)

private void OnDisable()
    {
        Debug.Log(_agent.cameraPlacementOffset);
        Debug.Log("are they equal? " + (serializedObject.FindProperty("cameraPlacementOffset").vector3Value == _agent.cameraPlacementOffset));
        serializedObject.FindProperty("cameraPlacementOffset").vector3Value = _agent.cameraPlacementOffset;
        Debug.Log(serializedObject.ApplyModifiedProperties());
        DisposeOfCameraStuff();
        CloseCameraWindow();
    }
gloomy chasm
#

The values are the same so no change is made so there is nothing to apply.

wintry trout
gloomy chasm
wintry trout
gloomy chasm
wintry trout
gloomy chasm
wintry trout
#

Sure I can

gloomy chasm
#

(I'm going to bed in a couple of minutes here, so sorry if I am a bit curt)

gloomy chasm
wintry trout
#

😦

#

So how do I do it

gloomy chasm
#

It makes things messy and is most likely what is causing the issue

gloomy chasm
# wintry trout So how do I do it

Any place you access _agent.cameraPlacementOffset you replace it with serializedObject.FindProperty("cameraPlacementOffset").vector3Value

#

Though you should cache the the Serialized property by getting it in OnEnable and setting a field to id so you can access it easier and faster.

wintry trout
#

Gonna try

wintry trout
#

Also now the changes to the value aren't shown in the inspector and can't be used by other code, such as prefab mode assistant camera

#

new variant, if you are still here

gloomy chasm
# wintry trout Changed it in saving and creating handles, doesn't work

Uh, something feels wrong but I can't quite say what right now. I'm too tired for this haha.
I will give you a bit of homework though. Don't touch target or anything to do with it. If you want a value (to get or set it) use serialized property. Otherwise it won't save property, won't support undo, and wont support overriding with prefab variants. So try switching it all over to that.

I will @ you when I get up tomorrow to see if you are still having trouble 🙂

soft briar
#

i have this bitmask enum

[Flags, EnumToggleButtons] public enum ActorTypes { Player = 1 << 1, Enemy = 1 << 2, Wall = 1 << 3, Spawner = 1 << 4 }

but i want if i selected Player , automatically Enemy and Wall turn off , or if i selected Enemy , automatically Player and Wall get turn off
the only thing that can be selected with others, should be Spawner , for example i able to select Wall and Spawner

#

i searched a lot, and did many trial & error, but couldn't make it

soft briar
#

i'm drawing with Odin , i tried what u said, but it didn't work

#

i will go with simple solution, spawner out of enum

tough cairn
#

how can i create nested scriptable object ?

tough cairn
#

awesome thanks

peak bloom
#

any idea why SerializedReference won't work with System.Object?

#

Do we need to make a wrapper for it to work?

peak bloom
#

I was blind! didn't see the Must not be a C# Value type

gloomy chasm
wintry trout
glad mica
#

what is the correct way to get this label text to align itself after where the label title would be, so in this picture start should be aligned to the start of the slider, and if the inspector gets resized it should stay at the start of the slider

wintry trout
#

@gloomy chasm haven't you forgotten about me by any way?

#

sorry if too persistent

glad mica
#

between 105-107 i think you didn't call it at all, so the change isnt updated

wintry trout
glad mica
#

OnDisable is called when you leave the inspector for that object, instead, call it at the last line of OnInspectorGUI()

wintry trout
#

lets try

glad mica
#

actually, i think you have other spots where you edited the serialised property, you should probably also call it at the end of OnSceneGUI()

glad mica
#

great

wintry trout
glad mica
#

probably you should add it as well, OnSceneGUI is called when the scene view is modified in editor mode, so if there is some instance where the inspector isnt used , such that OnInspectorGUI isnt called, but the scene is changed, the property may not be serialised if it is not applied

wintry trout
glad mica
#

you actually only need to call it once, at the end after you update any number of serialised properties. Serialising a process that Unity performs to save the value of the property somewhere, such that when you come to the same property, Unity puts the saved value back to show it to you. Once you leave the inspector, the editor values are temporarily lost/reset to default, but will be restored when Unity retrieves from its serialised data. Otherwise, if there is no serialised data, it will remain as it was. When you're changing the values/references, you're changing properties in script. Apply modified properties ensures that Unity performs the serialization process so that the references/values are retrievable when when come back to it. If it is not called, then the latest serialised value/references remains as it was before the change, which is why you were not able to see the value changing just now, because it was not serialised after the change

wintry trout
glad mica
#

if you are modifying values in the target script, then you're not using the serialised properties, you're just directly modifying the properties and hoping that unity serialises them properly. Like the other person said above, you probably should not mix access properties publicly with serialisable properties, as it does not always guarantee that the value will be serialised, although this may work for you now. Sometimes, when working with prefabs, directly accessing public properties and modifying them can cause the value to be reset to the prefab value when you leave and return to the inspector, instead of the modified value, if we do not use the serialized properties - it is not a good idea generally, I would still recommend using the serialised object and applying the changed properties to it

wintry trout
#

So when I apply changes the target changes

#

But when I change target, seralizedObject isn't affected...

glad mica
#

sometimes, it is possible that if you change target, the serializedObject may remain unaffected. It's not very consistent. But if you apply modified changes, unity will make sure to serialize all the modified fields for you, its an explicit way to ensure that your changes are saved

if you change only the serialisedProperty but do not apply modified changes, the change is registered by the target, but whenever the inspector updates again, it retrieves the latest serialised data that overwrites your intended change again

wintry trout
glad mica
#

the target is just your script

wintry trout
#

But why can't I just apply at the end?? Why do I have to do it right after modifying? What is the technical reason for it?

glad mica
#

you can apply it at the end

wintry trout
#

It doesn't work for me

glad mica
#

there's nothing wrong with applying at the end of the function call, but it must be somewhere after the change

wintry trout
#

Not at the end of function call, in OnDisable

glad mica
#

OnDisable is not immediately called after OnInspectorGUI or OnSceneChange, your changes will not be saved if you call it there only - your latest unsaved changes are being overwritten constantly by the latest serialised data (the one before the change) that unity has everytime it updates your inspector

wintry trout
#

If they are overwritten constantly, why do my Handles move?

glad mica
#

because... your handles are not using the serialised properties right? You're directly modifying the script properties and fortunately in this case, they are being serialised properly. But look at line 105-107, here you are using the serialised properties, but you did not apply them, so they are being overwritten by the last saved serialisation

#

wait hang on a second

#

no you're not directly changing the properties

glad mica
#

the position of the handles in scene is independent of your script

wintry trout
#

Let me tell you how I understand the topic so far

#

target is an instance of the inspected MB which is not supposed to be serialized (saved outside of RAM) but currently existing in RAM.
That's why I can change its values and use them.

serializedObject is the serialized data for the MB (probably in JSON or something similar).
So when I request data from it I do not get the changes by Handles unless I serialized this changes. That's why it is impossible to move Handles unless I serialize the data right after change.

So when I was directly modifying target and then trying to apply changes to serializedObject there was nothing to apply as non changes were made to the serialized data, only to the object in RAM

#

something like that, @glad mica

#

Actually, after writing it all down, it makes perfect sense

#

And probably once I apply changes to serializedObject target gets loaded from the new info

glad mica
#

yea, something like this

wintry trout
#

good

#

thank you @glad mica

gloomy chasm
wintry trout
#

Yea

gloomy chasm
gloomy chasm
coral epoch
#

I have an custom editor script that continues to cause ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint when altering a serialized property that is a list. It happens that this location:

if (units.Count > 0)
{
    EditorGUILayout.LabelField("Click to Delete");
    EditorGUILayout.BeginHorizontal();
...

I am not sure what other parts of the editor I need to share but it only happens in this script where I have a conditional based on the the property being updated

wintry trout
gloomy chasm
wintry trout
#

wtf

visual stag
wintry trout
gloomy chasm
wintry trout
gloomy chasm
wintry trout
#

nice

coral epoch
#

@visual stag so based on the code below where exactly do I put that ExitGUI?

...
if (!string.IsNullOrEmpty(addedUnit))
    {
        unitIds.Add(addedUnit);
        UpdateUnitList(unitIds);
        addedUnit = null;
    }
    if (serializedObject.ApplyModifiedProperties())
    {
        (target as UnitStack)?.UpdateMarker();
        GUIUtility.ExitGUI();
    }
...
private void UpdateUnitList(List<string>unitIds)
{
    _unitProperty.ClearArray();
    foreach (var unitId in unitIds)
    {
        _unitProperty.InsertArrayElementAtIndex(0);
        var unitIdEle = _unitProperty.GetArrayElementAtIndex(0);
        unitIdEle.stringValue = unitId;
    }
}
visual stag
#

Not sure what's going on, but put it at the end of the logic that you ran that led to the exception being called. I imagine you clicked something that changed the amount of things being drawn, so after that logic

coral epoch
#

This is actually being done at the end of OnInspectorGUI. One one call I open a dialog (EditorWindow) and select value that gets set to a variable in the editor script. On the next call that is when I apply that value. So literally where I have that Exit call is the end of inspector gui call

#

I can DM you the entire script if you want

visual stag
coral epoch
#

Ah cool

visual stag
#
if (remove != null)
{
    unitIds = unitIds.Where(id => id != remove).ToList();
    UpdateUnitList(unitIds);
    serializedObject.ApplyModifiedProperties();
    GUIUtility.ExitGUI();
}
#

Something like that

final pond
#

The Screenshotter. You know what it does

#

Just rotate the main camera to adjust the position

peak bloom
gloomy chasm
peak bloom
#

ah ok, so it's still a little hassle to work with.. I'll test the api.. Thanks my dude! 👍

glad mica
unborn bear
#

Im new to unity and acceindently did this how can i fix it

stark geyser
#

@unborn bear This is not related to editor extension. Reset scale and rotation.

lone halo
#

Hey, I am making an editor tool to find an object under the mouse.
But when I raycast in the SceneView it's not really accurate. The more to the right in the Scene view it is the more I have to aim off to the side of the object to hit it.
Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(Event.current.mousePosition);
Do I need to account for the SceneView resolution and position in the editor somehow?

#

Actually it's inaccurate even if I detach the Scene panel and fullscreen it. What might be causing the ray to miss so much?

thin fossil
#

Is it possible to add a button to the unity bottom toolbar?

cold portal
#

Tracking Custom Handles to Mouse

gloomy chasm
soft zenith
#

hi, does someone know a extension for this ? i don't wan't to write it from scratch as i'm pretty sure there's a good option around, however i could only find 1 and it's inspector are bugging, the list elements are overlapping

limpid linden
visual stag
# soft zenith

Upgrade your Unity version. Some versions do not have the fix, but it's an editor bug.

rare surge
#

Hi, I try to add EditorGUILayout.Foldout but it does not fit correctly. It is too far left. What can I do and move little bit? I added BeginHorizontal too see the full foldout.

peak bloom
#

Planning to make a custom 2D rigging tool of my own for my vscripting library

limpid linden
#

you can shift the indent over for the foldout (and then once more for its contents)

#

the GUIContent (the icon) does not get considered when it comes to adjusting positions in groups or containers

peak bloom
#

ah yes, finally.. thanks 👍

west drum
#

Does anyone have any resources on how to unit tests editor extensions?

I had an ObjectField in my code that was not updating the attribute it had to, because I forgot to write it. I would like to write a unit test, to make sure it doesnt happen again. But I have no idea how to unit test those things.

wanton monolith
#

I'm very confused about custom attributes and building. I have a custom attribute in an asmdef that is editor only. But I can't build my project because obviously, during building, the classes that use that attribute are throwing errors. How am I supposed to fix this?

    public class EnumButtonAttribute : PropertyAttribute
    {
        
    }
    [CustomPropertyDrawer(typeof(EnumButtonAttribute))]
    public class EnumButtonAttributeDrawer : PropertyDrawer
    {
    }

And my asmdef is like this:

visual stag
#

The drawer should be in the editor assembly, and the attribute the runtime

wanton monolith