[unity] Add some runtime instantiation methods.

This commit is contained in:
pharan 2017-05-15 12:48:50 +08:00
parent 90e1b5e89f
commit 549052e087
3 changed files with 62 additions and 2 deletions

View File

@ -57,7 +57,7 @@ namespace Spine.Unity {
return atlasAsset; return atlasAsset;
} }
/// <summary> /// <summary>
/// Creates a runtime AtlasAsset. Only providing the textures is slower because it has to search for atlas page matches. <seealso cref="Spine.Unity.AtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool)"/></summary> /// Creates a runtime AtlasAsset. Only providing the textures is slower because it has to search for atlas page matches. <seealso cref="Spine.Unity.AtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool)"/></summary>
public static AtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Shader shader, bool initialize) { public static AtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Shader shader, bool initialize) {
@ -94,12 +94,25 @@ namespace Spine.Unity {
materials[i] = mat; materials[i] = mat;
else else
throw new ArgumentException("Could not find matching atlas page in the texture array."); throw new ArgumentException("Could not find matching atlas page in the texture array.");
} }
// Create AtlasAsset normally // Create AtlasAsset normally
return CreateRuntimeInstance(atlasText, materials, initialize); return CreateRuntimeInstance(atlasText, materials, initialize);
} }
/// <summary>
/// Creates a runtime AtlasAsset. Only providing the textures is slower because it has to search for atlas page matches. <seealso cref="Spine.Unity.AtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool)"/></summary>
public static AtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Material materialPropertySource, bool initialize) {
var shader = materialPropertySource.shader;
var oa = CreateRuntimeInstance(atlasText, textures, shader, initialize);
foreach (var m in oa.materials) {
m.CopyPropertiesFromMaterial(materialPropertySource);
m.shaderKeywords = materialPropertySource.shaderKeywords;
}
return oa;
}
#endregion #endregion
void Reset () { void Reset () {

View File

@ -86,6 +86,23 @@ namespace Spine.Unity {
#endif #endif
#endregion #endregion
#region Runtime Instantiation
public static SkeletonGraphic NewSkeletonGraphicGameObject (SkeletonDataAsset skeletonDataAsset, Transform parent) {
SkeletonGraphic sg = SkeletonGraphic.AddSkeletonGraphicComponent(new GameObject("New Spine GameObject"), skeletonDataAsset);
if (parent != null) sg.transform.SetParent(parent, false);
return sg;
}
public static SkeletonGraphic AddSkeletonGraphicComponent (GameObject gameObject, SkeletonDataAsset skeletonDataAsset) {
var c = gameObject.AddComponent<SkeletonGraphic>();
if (skeletonDataAsset != null) {
c.skeletonDataAsset = skeletonDataAsset;
c.Initialize(false);
}
return c;
}
#endregion
#region Internals #region Internals
// This is used by the UI system to determine what to put in the MaterialPropertyBlock. // This is used by the UI system to determine what to put in the MaterialPropertyBlock.
public override Texture mainTexture { public override Texture mainTexture {

View File

@ -71,6 +71,36 @@ namespace Spine.Unity.Modules {
#endif #endif
#endregion #endregion
#region Runtime Instantiation
public static SkeletonRenderSeparator AddToSkeletonRenderer (SkeletonRenderer skeletonRenderer, int sortingLayerID = 0, int extraPartsRenderers = 0, int sortingOrderIncrement = DefaultSortingOrderIncrement, int baseSortingOrder = 0, bool addMinimumPartsRenderers = true) {
if (skeletonRenderer == null) {
Debug.Log("Tried to add SkeletonRenderSeparator to a null SkeletonRenderer reference.");
return null;
}
var srs = skeletonRenderer.gameObject.AddComponent<SkeletonRenderSeparator>();
srs.skeletonRenderer = skeletonRenderer;
skeletonRenderer.Initialize(false);
int count = extraPartsRenderers;
if (addMinimumPartsRenderers)
count = extraPartsRenderers + skeletonRenderer.separatorSlots.Count + 1;
var skeletonRendererTransform = skeletonRenderer.transform;
var componentRenderers = srs.partsRenderers;
for (int i = 0; i < count; i++) {
var smr = SkeletonPartsRenderer.NewPartsRendererGameObject(skeletonRendererTransform, i.ToString());
var mr = smr.MeshRenderer;
mr.sortingLayerID = sortingLayerID;
mr.sortingOrder = baseSortingOrder + (i * sortingOrderIncrement);
componentRenderers.Add(smr);
}
return srs;
}
#endregion
void OnEnable () { void OnEnable () {
if (skeletonRenderer == null) return; if (skeletonRenderer == null) return;
if (copiedBlock == null) copiedBlock = new MaterialPropertyBlock(); if (copiedBlock == null) copiedBlock = new MaterialPropertyBlock();