2019-04-17 06:38:10 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
2019-10-13 16:47:22 -04:00
|
|
|
|
namespace UnityAtoms.SceneMgmt.Editor
|
2019-04-17 06:38:10 -04:00
|
|
|
|
{
|
2019-10-15 17:31:21 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Customer property drawer for `SceneField`.
|
|
|
|
|
/// </summary>
|
2019-04-17 06:38:10 -04:00
|
|
|
|
[CustomPropertyDrawer(typeof(SceneField))]
|
|
|
|
|
public class SceneFieldDrawer : PropertyDrawer
|
|
|
|
|
{
|
|
|
|
|
private bool HasValidBuildIndex(SerializedProperty property)
|
|
|
|
|
{
|
2019-05-07 16:37:50 -04:00
|
|
|
|
var scene = property.FindPropertyRelative("_sceneAsset")?.objectReferenceValue;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
if (scene == null) return true;
|
|
|
|
|
var scenePath = AssetDatabase.GetAssetPath(scene);
|
|
|
|
|
var buildIndex = SceneUtility.GetBuildIndexByScenePath(scenePath);
|
|
|
|
|
return buildIndex != -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
|
|
|
{
|
|
|
|
|
EditorGUI.BeginProperty(position, label, property);
|
|
|
|
|
OnPropertyGUI(position, property, label);
|
|
|
|
|
EditorGUI.EndProperty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
|
|
|
|
{
|
2019-05-04 05:54:20 -04:00
|
|
|
|
if (HasValidBuildIndex(property)) return base.GetPropertyHeight(property, label);
|
2019-04-17 06:38:10 -04:00
|
|
|
|
return base.GetPropertyHeight(property, label) * 2 + 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPropertyGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
|
|
|
{
|
|
|
|
|
Rect lower;
|
|
|
|
|
Rect buttonRect = new Rect();
|
2019-05-07 16:37:50 -04:00
|
|
|
|
var scene = property.FindPropertyRelative("_sceneAsset")?.objectReferenceValue;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
|
2019-05-04 05:54:20 -04:00
|
|
|
|
if (scene == null)
|
2019-05-07 16:37:50 -04:00
|
|
|
|
{
|
|
|
|
|
// Update values cause the build index could've changed
|
|
|
|
|
property.FindPropertyRelative("_sceneName").stringValue = "";
|
|
|
|
|
property.FindPropertyRelative("_scenePath").stringValue = "";
|
|
|
|
|
property.FindPropertyRelative("_buildIndex").intValue = -1;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 16:37:50 -04:00
|
|
|
|
position = IMGUIUtils.SnipRectV(position, EditorGUIUtility.singleLineHeight, out lower, 2f);
|
2019-05-04 05:54:20 -04:00
|
|
|
|
if (HasValidBuildIndex(property))
|
|
|
|
|
{
|
|
|
|
|
if (scene != null)
|
2019-05-07 16:37:50 -04:00
|
|
|
|
{
|
|
|
|
|
// Update values cause the build index could've changed
|
|
|
|
|
property.FindPropertyRelative("_sceneName").stringValue = scene.name;
|
|
|
|
|
property.FindPropertyRelative("_scenePath").stringValue = AssetDatabase.GetAssetPath(scene);
|
|
|
|
|
property.FindPropertyRelative("_buildIndex").intValue = SceneUtility.GetBuildIndexByScenePath(
|
|
|
|
|
property.FindPropertyRelative("_scenePath").stringValue
|
2019-04-17 06:38:10 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-05-04 05:54:20 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
2019-04-17 06:38:10 -04:00
|
|
|
|
{
|
2019-05-07 16:37:50 -04:00
|
|
|
|
position = IMGUIUtils.SnipRectH(position, position.width - 70, out buttonRect, 6f);
|
|
|
|
|
property.FindPropertyRelative("_buildIndex").intValue = -1;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SceneAsset sceneAsset = scene as SceneAsset;
|
|
|
|
|
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
|
|
|
|
|
|
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
|
|
|
scene = EditorGUI.ObjectField(position, scene, typeof(SceneAsset), false);
|
|
|
|
|
if (EditorGUI.EndChangeCheck())
|
|
|
|
|
{
|
2019-05-07 16:37:50 -04:00
|
|
|
|
property.FindPropertyRelative("_sceneAsset").objectReferenceValue = scene;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
sceneAsset = scene as SceneAsset;
|
|
|
|
|
if (sceneAsset != null)
|
|
|
|
|
{
|
2019-05-07 16:37:50 -04:00
|
|
|
|
property.FindPropertyRelative("_sceneName").stringValue = scene.name;
|
|
|
|
|
property.FindPropertyRelative("_scenePath").stringValue = AssetDatabase.GetAssetPath(scene);
|
|
|
|
|
property.FindPropertyRelative("_buildIndex").intValue = SceneUtility.GetBuildIndexByScenePath(
|
|
|
|
|
property.FindPropertyRelative("_scenePath").stringValue
|
2019-04-17 06:38:10 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 16:37:50 -04:00
|
|
|
|
if (property.FindPropertyRelative("_buildIndex").intValue != -1) return;
|
2019-04-17 06:38:10 -04:00
|
|
|
|
|
|
|
|
|
if (scene != null && scene is SceneAsset)
|
|
|
|
|
{
|
|
|
|
|
EditorGUI.HelpBox(lower, "Scene is not added in the build settings", MessageType.Warning);
|
|
|
|
|
if (GUI.Button(buttonRect, "Fix Now"))
|
|
|
|
|
{
|
|
|
|
|
AddSceneToBuildSettings(sceneAsset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddSceneToBuildSettings(SceneAsset sceneAsset)
|
|
|
|
|
{
|
|
|
|
|
var scenePath = AssetDatabase.GetAssetPath(sceneAsset);
|
|
|
|
|
|
|
|
|
|
var scenes = EditorBuildSettings.scenes.ToList();
|
|
|
|
|
scenes.Add(new EditorBuildSettingsScene(scenePath, true));
|
|
|
|
|
|
|
|
|
|
EditorBuildSettings.scenes = scenes.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|