Skip to content

Instantly share code, notes, and snippets.

@LukasKastern
Last active September 28, 2020 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukasKastern/10b6a36974f3856aa5d6708793edaac3 to your computer and use it in GitHub Desktop.
Save LukasKastern/10b6a36974f3856aa5d6708793edaac3 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using Hash128 = Unity.Entities.Hash128;
#if UNITY_EDITOR
using UnityEditor;
[CustomPropertyDrawer( typeof( SubSceneReference ) )]
public class SubSceneReferencePropertyDrawer : PropertyDrawer
{
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
{
EditorGUI.BeginProperty( position, GUIContent.none, property );
var sceneObject = property.FindPropertyRelative( "sceneAsset" ).objectReferenceValue;
{
//Draw scene asset
var sceneAsset = property.FindPropertyRelative( "sceneAsset" );
position = EditorGUI.PrefixLabel( position, GUIUtility.GetControlID( FocusType.Passive ), label );
if ( sceneAsset != null )
{
sceneAsset.objectReferenceValue = EditorGUI.ObjectField( position, sceneAsset.objectReferenceValue,
typeof( SceneAsset ), false );
}
}
var sceneHash = property.FindPropertyRelative( "SceneReference" );
var hash = default( Hash128 );
if ( sceneObject != null )
{
var sceneGuid = new GUID( AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( sceneObject ) ) );
hash = sceneGuid;
}
var value = sceneHash.FindPropertyRelative( "Value" );
if ( value == null ) throw new Exception( "Failed to retrieve value property from SubSceneReference" );
value.FindPropertyRelative( "x" ).longValue = ( long ) hash.Value.x;
value.FindPropertyRelative( "y" ).longValue = ( long ) hash.Value.y;
value.FindPropertyRelative( "z" ).longValue = ( long ) hash.Value.z;
value.FindPropertyRelative( "w" ).longValue = ( long ) hash.Value.w;
EditorGUI.EndProperty( );
}
}
#endif
[Serializable]
public class SubSceneReference
{
public static implicit operator Hash128( SubSceneReference reference ) => reference.SceneReference;
#if UNITY_EDITOR
[SerializeField]
SceneAsset sceneAsset;
#endif
public Hash128 SceneReference;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment