Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Scene Variable, Reference, and Collection #57

Conversation

jeffcampbellmakesgames
Copy link
Contributor

@jeffcampbellmakesgames jeffcampbellmakesgames commented Apr 17, 2019

Summary

The goal of this PR is to create a new Variable, Reference, and Collection type that allows for referencing a SceneAsset indirectly (as its an Editor-only type) by capturing only the relevant, serializable information about it. It allows for a user to assign a SceneAsset directly to a SceneVariable or SceneReference as a constant and its full scene name, index in the build settings, and enabled status in the build settings are preserved in the ScriptableObject as a POCO SceneInfo type..

Variable Declaration Stub

    public sealed class SceneVariable : BaseVariable<SceneInfo>
    {
...
    }

Example code

    public class LoadSceneOnAwake : MonoBehaviour
    {
        public SceneReference sceneReference;

        private void Awake()
        {
            if (sceneReference.Value.IsSceneEnabled && sceneReference.Value.IsSceneInBuildSettings)
            {
                SceneManager.LoadScene(sceneReference.Value.SceneName, LoadSceneMode.Additive);
            }
        }
    }

Additionally I have added a custom inspector and property drawer for SceneVariable and SceneInfo that allows for a user to assign a SceneAsset directly, but only preserve the relevant info. On the SceneVariable inspector, warnings will be shown if a scene is not assigned, its not in the build settings, or if its in the build settings and disabled.

SceneVariableInspector

A small feature that was made to the BaseReferenceDrawer as a part of this is some dynamic behavior regarding the size of the constant property that is being shown. If it is less than or equal to a single line width, the current behavior is preserved to show the property field to the right of the label and popup selector. If it is larger than a single line height, it will be bumped to the next line and take up the full width of the inspector. This is useful for Reference classes whose generic T type that will be shown in a property field cannot easily be shown in the smaller width to the right of the label and needs the full width of the inspector. Indenting was also fixed so that SceneReferences in a list or array would be indented and aligned with their indexed element.

DynamicBaseReferencePropertyDrawer

I've also added custom icons for the SceneVariable and SceneCollection ScriptableObject classes that matches the existing style for other icons. I did make the size of these icons at 128x128 rather than 16x16 so that the displayed Unity icon portion of it was legible. If better versions of these could be made at the smaller resolution I'm more than happy to discard these.

image

Testing

I've added an additional commit to the head of this branch that should make it easier to test this PR without additional work. Loading scene Assets/PR Test Content/SceneVariableTestScene01.unity and playing in the editor should load Assets/PR Test Content/SceneVariableTestScene02.unity additively on Awake. There is an SceneVariable and SceneCollection instance that can be played around with. The asset creation menus for these types should also be working and present in the right places.

This commit should be discarded prior to merging.

Changes

Created Scene Variable, Collection, and Reference

  • Created SceneVariable as a generic BaseVariable of type SceneInfo, where SceneInfo contains serializable information about a scene asset.
  • Created SceneVariableEditor to allow for custom inspection of SceneVariable as well as SceneInfoPropertyDrawer for the serializable value type. This allows for a user to assign a SceneAsset to an object field and ping it while only the serialized relevant values (full path, index and enabled status in build settings) are persisted in the ScriptableObject. Helpful warnings are shown for the user if a scene is not assigned, present in the build settings, or enabled in the build settings.
  • Created SceneCollection
  • Created AssemblyInfo.cs as a code file for assembly attributes for the ScriptableObject-Architecture Assembly Definition. The current assembly attribute allows for the internals of this assembly to be visible to ScriptableObject-Architecture.Editor. This allows for internal types and members to be used in inspectors, editor windows without directly exposing them to end-users.

Modified base Variable and Reference Drawer

  • Modified DrawValue method of BaseVariableDrawer to be virtual.
  • Modified BaseReferenceDrawer to check if constant property height is greater than a single line, and if it is to display it inline below the field rather than to the right of the label. For existing value type reference types this will not change their UX, but for more complex types it allows for more space to be displayed across the entire width of the inspector. Popup will display as normal.

@DanielEverland DanielEverland added this to the Release 1.6.0 milestone Apr 18, 2019
@DanielEverland
Copy link
Owner

I really like this PR, but I'm unfortunately having some issues with your property drawer

First of all, when in an array, the Reference button for changing between a variable and constant doesn't always register input. If I checkout master and create an array of, say, FloatReference's, the submenu button always to being pressed.

You can't see my mouse input in this gif, obviously, but when it's over the submenu I'm always spamming left click. It takes several tries to get it to respond

Second of all, I really like the indenting feature for complex drawers like the one you've implemented. It has the unintended effect of creating a fairly tall drawer for simple types like floats, though.

master

PR

@DanielEverland
Copy link
Owner

The new icon doesn't scale properly in the inspector, I think it has to be the same size as the other icons. You don't have to fix that, though, I'll make sure it looks good prior to merging with master :)

@jeffcampbellmakesgames
Copy link
Contributor Author

First of all, when in an array, the Reference button for changing between a variable and constant doesn't always register input. If I checkout master and create an array of, say, FloatReference's, the submenu button always to being pressed.

You can't see my mouse input in this gif, obviously, but when it's over the submenu I'm always spamming left click. It takes several tries to get it to respond

Thanks for the feedback, I'll take a look at this behavior and rectify it. What I noticed about using the popup style button is that it really responds to clicks on the little arrow and nothing else (not on the hamburger icon). I'm not sure what is aggravating that, but I will fix it one way or the other.

Second of all, I really like the indenting feature for complex drawers like the one you've implemented. It has the unintended effect of creating a fairly tall drawer for simple types like floats, though.

This is probably a side effect of using GetPropertyHeight to set the size of the rect and somehow the size is being misreported. This should be fairly easy to fix.

The new icon doesn't scale properly in the inspector, I think it has to be the same size as the other icons. You don't have to fix that, though, I'll make sure it looks good prior to merging with master :)

Word, thanks for taking a look at that. I am not an UI artist by any means, but I thought I'd take a first crack at it to make things easier.

jzapdot added 2 commits April 18, 2019 15:53
* Modified DrawValue method of BaseVariableDrawer to be virtual.
* Modified BaseReferenceDrawer to check if constant property height is greater than a single line, and if it is to display it inline below the field rather than to the right of the label. For existing value type reference types this will not change their UX, but for more complex types it allows for more space to be displayed across the entire width of the inspector. Popup will display as normal.
* Created SceneVariable as a generic BaseVariable of type SceneInfo, where SceneInfo contains serializable information about a scene asset.
* Created SceneVariableEditor to allow for custom inspection of SceneVariable as well as SceneInfoPropertyDrawer for the serializable value type. This allows for a user to assign a SceneAsset to an object field and ping it while only the serialized relevant values (full path, index and enabled status in build settings) are persisted in the ScriptableObject. Helpful warnings are shown for the user if a scene is not assigned, present in the build settings, or enabled in the build settings.
* Created SceneCollection
* Created AssemblyInfo.cs as a code file for assembly attributes for the ScriptableObject-Architecture Assembly Definition. The current assembly attribute allows for the internals of this assembly to be visible to ScriptableObject-Architecture.Editor. This allows for internal types and members to be used in inspectors, editor windows without directly exposing them to end-users.
jzapdot added 3 commits April 19, 2019 08:39
* Modified GetPropertyHeight in BaseReferenceDrawer to only use the inline height calculation if the constant value property height would be greater than a single line.
* Modified the width of the popup style button to be a constant fixed width of 25f. This style seems to have an issue when shown in a list that its normal fixed width is obscuring the clickable element and buffing this width to a fixed size makes this area clickable again.
* This is content added to make it easier to test this PR in the editor, it can be discarded after reviewing
@jeffcampbellmakesgames
Copy link
Contributor Author

@DanielEverland Those reference drawer issues should now be resolved with the commit titled PR Fixes There seem to be some issues with that particular popup style in lists that even without the custom code I added was making it difficult to click. By setting it to a constant fixed width that increases its area this seems to have resolved that.

I've also tweaked the usage of GetPropertyHeight so that simple value types or those whose height is only one line or less are shown adjacent to the label rather than inline below.

BaseReferenceDrawerFixes

@DanielEverland
Copy link
Owner

Merged, not sure why GitHub didn't register automatically.

@jeffcampbellmakesgames jeffcampbellmakesgames deleted the feat/scene_variable branch April 21, 2019 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants