[unity] Centralize creating GameObjects in the editor using SpineEditorUtilities.EditorInstantiation.

This commit is contained in:
pharan 2018-12-28 22:13:42 +08:00
parent 0aa617c44a
commit 40f5755fe0
7 changed files with 65 additions and 16 deletions

View File

@ -46,7 +46,7 @@ namespace Spine.Unity.Editor {
[MenuItem ("CONTEXT/SkeletonRenderer/Add BoneFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer;
var go = new GameObject("New BoneFollower");
var go = SpineEditorUtilities.EditorInstantiation.NewGameObject("New BoneFollower");
var t = go.transform;
t.SetParent(skeletonRenderer.transform);
t.localPosition = Vector3.zero;

View File

@ -46,7 +46,7 @@ namespace Spine.Unity.Editor {
[MenuItem("CONTEXT/SkeletonRenderer/Add PointFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer;
var go = new GameObject("PointFollower");
var go = SpineEditorUtilities.EditorInstantiation.NewGameObject("PointFollower");
var t = go.transform;
t.SetParent(skeletonRenderer.transform);
t.localPosition = Vector3.zero;

View File

@ -296,7 +296,7 @@ namespace Spine.Unity.Editor {
}
}
GameObject prefabRoot = new GameObject("root");
GameObject prefabRoot = SpineEditorUtilities.EditorInstantiation.NewGameObject("root");
Dictionary<string, Transform> slotTable = new Dictionary<string, Transform>();
Dictionary<string, Transform> boneTable = new Dictionary<string, Transform>();
@ -305,7 +305,7 @@ namespace Spine.Unity.Editor {
//create bones
for (int i = 0; i < skeletonData.Bones.Count; i++) {
var boneData = skeletonData.Bones.Items[i];
Transform boneTransform = new GameObject(boneData.Name).transform;
Transform boneTransform = SpineEditorUtilities.EditorInstantiation.NewGameObject(boneData.Name).transform;
boneTransform.parent = prefabRoot.transform;
boneTable.Add(boneTransform.name, boneTransform);
boneList.Add(boneTransform);
@ -336,7 +336,7 @@ namespace Spine.Unity.Editor {
//create slots and attachments
for (int i = 0; i < skeletonData.Slots.Count; i++) {
var slotData = skeletonData.Slots.Items[i];
Transform slotTransform = new GameObject(slotData.Name).transform;
Transform slotTransform = SpineEditorUtilities.EditorInstantiation.NewGameObject(slotData.Name).transform;
slotTransform.parent = prefabRoot.transform;
slotTable.Add(slotData.Name, slotTransform);
@ -392,7 +392,7 @@ namespace Spine.Unity.Editor {
} else
continue;
Transform attachmentTransform = new GameObject(attachmentName).transform;
Transform attachmentTransform = SpineEditorUtilities.EditorInstantiation.NewGameObject(attachmentName).transform;
attachmentTransform.parent = slotTransform;
attachmentTransform.localPosition = offset;
@ -1429,7 +1429,7 @@ namespace Spine.Unity.Editor {
Directory.CreateDirectory(bakedDirPath);
if (prefab == null) {
root = new GameObject("temp", typeof(MeshFilter), typeof(MeshRenderer));
root = SpineEditorUtilities.EditorInstantiation.NewGameObject("temp", typeof(MeshFilter), typeof(MeshRenderer));
#if NEW_PREFAB_SYSTEM
prefab = PrefabUtility.SaveAsPrefabAsset(root, bakedPrefabPath);
#else

View File

@ -38,6 +38,10 @@
#define NEWPLAYMODECALLBACKS
#endif
#if UNITY_2018_3 || UNITY_2019
#define NEW_PREFAB_SYSTEM
#endif
#if UNITY_2018 || UNITY_2019
#define NEWHIERARCHYWINDOWCALLBACKS
#endif
@ -1274,7 +1278,7 @@ namespace Spine.Unity.Editor {
}
string spineGameObjectName = string.Format("Spine GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", ""));
GameObject go = new GameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation));
GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation));
SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>();
newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset;
TryInitializeSkeletonRendererSettings(newSkeletonAnimation, skin);
@ -1298,12 +1302,30 @@ namespace Spine.Unity.Editor {
return newSkeletonAnimation;
}
/// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary>
public static GameObject NewGameObject (string name) {
#if NEW_PREFAB_SYSTEM
return ObjectFactory.CreateGameObject(gameObjectName);
#else
return new GameObject(name);
#endif
}
/// <summary>Handles creating a new GameObject in the Unity Editor. This uses the new ObjectFactory API where applicable.</summary>
public static GameObject NewGameObject (string name, params System.Type[] components) {
#if NEW_PREFAB_SYSTEM
return ObjectFactory.CreateGameObject(gameObjectName, components);
#else
return new GameObject(name, components);
#endif
}
public static void InstantiateEmptySpineGameObject<T> (string name) where T : MonoBehaviour {
var parentGameObject = Selection.activeObject as GameObject;
var parentTransform = parentGameObject == null ? null : parentGameObject.transform;
var gameObject = new GameObject(name, typeof(T));
var gameObject = EditorInstantiation.NewGameObject(name, typeof(T));
gameObject.transform.SetParent(parentTransform, false);
EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject;
@ -1333,7 +1355,7 @@ namespace Spine.Unity.Editor {
}
string spineGameObjectName = string.Format("Spine Mecanim GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", ""));
GameObject go = new GameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(Animator), typeof(SkeletonMecanim));
GameObject go = EditorInstantiation.NewGameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(Animator), typeof(SkeletonMecanim));
if (skeletonDataAsset.controller == null) {
SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset);

View File

@ -192,7 +192,7 @@ namespace Spine.Unity.Editor {
#endregion
static GameObject AddBoundingBoxFollowerChild (SkeletonRenderer sr, BoundingBoxFollower original = null) {
var go = new GameObject("BoundingBoxFollower");
var go = SpineEditorUtilities.EditorInstantiation.NewGameObject("BoundingBoxFollower");
go.transform.SetParent(sr.transform, false);
var newFollower = go.AddComponent<BoundingBoxFollower>();

View File

@ -1,8 +1,35 @@
using System.Collections;
using System.Collections.Generic;
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using UnityEngine;
using UnityEditor;
using Spine.Unity;
namespace Spine.Unity.Editor {
@ -21,7 +48,7 @@ namespace Spine.Unity.Editor {
[MenuItem ("CONTEXT/SkeletonGraphic/Add BoneFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonGraphic = cmd.context as SkeletonGraphic;
var go = new GameObject("BoneFollower", typeof(RectTransform));
var go = SpineEditorUtilities.EditorInstantiation.NewGameObject("BoneFollower", typeof(RectTransform));
var t = go.transform;
t.SetParent(skeletonGraphic.transform);
t.localPosition = Vector3.zero;

View File

@ -206,7 +206,7 @@ namespace Spine.Unity.Editor {
}
static GameObject NewSkeletonGraphicGameObject (string gameObjectName) {
var go = new GameObject(gameObjectName, typeof(RectTransform), typeof(CanvasRenderer), typeof(SkeletonGraphic));
var go = SpineEditorUtilities.EditorInstantiation.NewGameObject(gameObjectName, typeof(RectTransform), typeof(CanvasRenderer), typeof(SkeletonGraphic));
var graphic = go.GetComponent<SkeletonGraphic>();
graphic.material = SkeletonGraphicInspector.DefaultSkeletonGraphicMaterial;
return go;