这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

95 行
2.3 KiB

using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
[CustomEditor(typeof(GameScene), true)]
public class GameSceneEditor : Editor
{
#region UI_Warnings
private const string noScenesWarning = "There are no scenes set for this level yet! Add a new scene with the dropdown below";
#endregion
#region GUI_Styles
private GUIStyle headerLabelStyle;
#endregion
// it holds a list of properties to hide from basic inspector
private static readonly string[] ExcludedProperties = { "m_Script", "sceneName" };
private string[] sceneList;
private GameScene gameSceneTarget;
private void OnEnable()
{
gameSceneTarget = target as GameScene;
PopulateScenePicker();
InitializeGuiStyles();
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Scene information", headerLabelStyle);
EditorGUILayout.Space();
DrawScenePicker();
DrawPropertiesExcluding(serializedObject, ExcludedProperties);
}
private void DrawScenePicker()
{
var sceneName = gameSceneTarget.sceneName;
EditorGUI.BeginChangeCheck();
var selectedScene = sceneList.ToList().IndexOf(sceneName);
if (selectedScene < 0)
{
EditorGUILayout.HelpBox(noScenesWarning, MessageType.Warning);
}
selectedScene = EditorGUILayout.Popup("Scene", selectedScene, sceneList);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(target, "Changed selected scene");
gameSceneTarget.sceneName = sceneList[selectedScene];
MarkAllDirty();
}
}
private void InitializeGuiStyles()
{
headerLabelStyle = new GUIStyle(EditorStyles.largeLabel)
{
fontStyle = FontStyle.Bold,
fontSize = 18,
fixedHeight = 70.0f
};
}
/// <summary>
/// Populates the scene picker with scenes included in the game's build index
/// </summary>
private void PopulateScenePicker()
{
var sceneCount = SceneManager.sceneCountInBuildSettings;
sceneList = new string[sceneCount];
for (int i = 0; i < sceneCount; i++)
{
sceneList[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
}
}
/// <summary>
/// Marks scenes as dirty so data can be saved
/// </summary>
private void MarkAllDirty()
{
EditorUtility.SetDirty(target);
EditorSceneManager.MarkAllScenesDirty();
}
}