浏览代码

Merge pull request #2 from peeweek/feature/new-scene-from-template

Feature/new scene from template
/main
GitHub 6 年前
当前提交
0a6ba7db
共有 6 个文件被更改,包括 280 次插入0 次删除
  1. 8
      Editor/NewSceneWindow.meta
  2. 11
      Editor/NewSceneWindow/NewSceneWindow.cs.meta
  3. 11
      Editor/NewSceneWindow/SceneTemplateList.cs.meta
  4. 220
      Editor/NewSceneWindow/NewSceneWindow.cs
  5. 30
      Editor/NewSceneWindow/SceneTemplateList.cs

8
Editor/NewSceneWindow.meta


fileFormatVersion: 2
guid: abc8c99e8458f7f468eee26216bd1879
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

11
Editor/NewSceneWindow/NewSceneWindow.cs.meta


fileFormatVersion: 2
guid: 6af9ca5ec6a20d3488386a4eb9919ed9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
Editor/NewSceneWindow/SceneTemplateList.cs.meta


fileFormatVersion: 2
guid: 261311c8711012d489943f40ba26402d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

220
Editor/NewSceneWindow/NewSceneWindow.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
namespace GameplayIngredients.Editor
{
public class NewSceneWindow : EditorWindow
{
const int WindowWidth = 640;
const int WindowHeight = 380;
[MenuItem("File/New Scene From Template... &%N", priority = 150)]
static void ShowNewSceneWindow()
{
GetWindow<NewSceneWindow>(true);
}
private void OnEnable()
{
titleContent = new GUIContent("New Scene From Template");
this.position = new Rect((Screen.width / 2.0f) - WindowWidth / 2, (Screen.height / 2.0f) - WindowHeight / 2, WindowWidth, WindowHeight);
this.minSize = new Vector2(WindowWidth, WindowHeight);
this.maxSize = new Vector2(WindowWidth, WindowHeight);
ReloadList();
}
private void OnGUI()
{
using (new GUILayout.HorizontalScope())
{
GUILayout.Label("Available Templates", EditorStyles.boldLabel, GUILayout.Width(188));
GUILayout.Label("Description", EditorStyles.boldLabel);
}
using (new GUILayout.HorizontalScope(GUILayout.ExpandHeight(true)))
{
using (new GUILayout.VerticalScope(Styles.listBox, GUILayout.Width(180)))
{
DrawTemplateList();
}
GUILayout.Space(8);
using (new GUILayout.VerticalScope(Styles.detailBox))
{
DrawTemplateDetails();
}
}
using (new GUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
EditorGUI.BeginDisabledGroup(selectedTemplate.Scene == null);
if (GUILayout.Button(" Create ", Styles.buttonLeft))
{
// Creates a new map and replaces the current setup
CreateSceneFromTemplate(selectedTemplate.Scene);
this.Close();
}
if (GUILayout.Button(" Append ", Styles.buttonRight))
{
bool canCreateScene = false;
if (EditorSceneManager.GetActiveScene().path == string.Empty)
{
// We need to save active scene First
if(EditorUtility.DisplayDialog("Create new Scene", "You can only append a new scene if the current active scene has been saved, do you want to save it first?","Yes","No"))
{
string path = EditorUtility.SaveFilePanelInProject("Save Scene", "New Scene", "unity", "New Scene");
if(path != string.Empty)
{
if(EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), path))
canCreateScene = true;
}
}
}
if(canCreateScene)
{
// Creates a new map that is added to the current setup
CreateSceneFromTemplate(selectedTemplate.Scene,true);
this.Close();
}
}
EditorGUI.EndDisabledGroup();
}
}
static void CreateSceneFromTemplate(SceneAsset template, bool append = false)
{
var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, append? NewSceneMode.Additive: NewSceneMode.Single);
var temp = EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(template), OpenSceneMode.Additive);
var objects = temp.GetRootGameObjects();
foreach(var obj in objects)
{
SceneManager.MoveGameObjectToScene(obj, newScene);
}
EditorSceneManager.CloseScene(temp, true);
}
public static void ReloadList()
{
if (templateLists == null)
templateLists = new List<SceneTemplateList>();
else
templateLists.Clear();
string[] all = AssetDatabase.FindAssets("t:" + typeof(SceneTemplateList).Name);
foreach(var guid in all)
{
templateLists.Add(AssetDatabase.LoadAssetAtPath<SceneTemplateList>(AssetDatabase.GUIDToAssetPath(guid)));
}
}
static List<SceneTemplateList> templateLists;
static SceneWindowTemplate selectedTemplate;
Vector2 scrollList = Vector2.zero;
void DrawTemplateList()
{
GUILayout.BeginScrollView(scrollList);
int i = 0;
foreach(var list in templateLists)
{
if(i > 0)
{
GUILayout.Space(8);
}
GUILayout.Label(list.ListName, Styles.listHeader);
EditorGUI.indentLevel++;
foreach (var template in list.Templates)
{
if(GUILayout.Button(template.Name, Styles.listItem))
{
selectedTemplate = template;
}
if(template.Name == selectedTemplate.Name && template.Scene == selectedTemplate.Scene)
{
Rect hl = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(hl, new Color(1, 1, 1, 0.05f));
}
}
EditorGUI.indentLevel--;
i++;
}
GUILayout.FlexibleSpace();
GUILayout.EndScrollView();
}
void DrawTemplateDetails()
{
if(selectedTemplate.Scene != null)
{
GUILayout.Label(selectedTemplate.Name, Styles.detailsTitle);
GUILayout.Space(16);
GUILayout.Label(selectedTemplate.Description, Styles.detailsBody);
}
else
{
GUILayout.Label("No Template Selected");
}
GUILayout.FlexibleSpace();
}
static class Styles
{
public static GUIStyle buttonLeft;
public static GUIStyle buttonMid;
public static GUIStyle buttonRight;
public static GUIStyle listBox;
public static GUIStyle listHeader;
public static GUIStyle listItem;
public static GUIStyle detailBox;
public static GUIStyle detailsTitle;
public static GUIStyle detailsBody;
static Styles()
{
buttonLeft = new GUIStyle(EditorStyles.miniButtonLeft);
buttonMid = new GUIStyle(EditorStyles.miniButtonMid);
buttonRight = new GUIStyle(EditorStyles.miniButtonRight);
buttonLeft.fontSize = 12;
buttonMid.fontSize = 12;
buttonRight.fontSize = 12;
listHeader = new GUIStyle("ShurikenModuleTitle");
listHeader.padding= new RectOffset(0,0,2,2);
listHeader.fontSize = 12;
listHeader.stretchWidth = false;
listHeader.fixedWidth = 172;
listHeader.stretchHeight = true;
listHeader.fixedHeight = 24;
listBox = new GUIStyle(EditorStyles.helpBox);
detailBox = new GUIStyle(listBox);
detailBox.padding = new RectOffset(12, 12, 12, 12);
listItem = new GUIStyle(EditorStyles.label);
listItem.padding = new RectOffset(16, 0, 1, 1);
detailsTitle = new GUIStyle(EditorStyles.label);
detailsTitle.fontSize = 24;
detailsBody = new GUIStyle(EditorStyles.wordWrappedLabel);
detailsBody.fontSize = 14;
}
}
}
}

30
Editor/NewSceneWindow/SceneTemplateList.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace GameplayIngredients.Editor
{
public class SceneTemplateList : ScriptableObject
{
[MenuItem("Assets/Create/Scene Template List", priority = 201)]
static void CreateSceneTemplateListAsset()
{
AssetFactory.CreateAssetInProjectWindow<SceneTemplateList>("", "New SceneTemplateList.asset");
}
public string ListName = "New Template List";
public SceneWindowTemplate[] Templates;
}
[System.Serializable]
public struct SceneWindowTemplate
{
public string Name;
[Multiline]
public string Description;
[NonNullCheck]
public SceneAsset Scene;
}
}
正在加载...
取消
保存