unity lua加载spine代码
c#:using UnityEngine;using XLua;
using System.Collections.Generic;
using System.Collections;
using System;
using UnityEngine.Sprites;
using Spine.Unity;
namespace UnityEngine{
/// <summary>
/// 自定义一个工具类
/// </summary>
public class InstanceTool{
/// <summary>
/// 加载Resources文件下制定路径及名字的Sprite图片
/// </summary>
public static Sprite ResourcesSprite(string path)
{
return Resources.Load<Sprite>(path);
}
/// <summary>
/// 加载Material lua里没反射
/// </summary>
public static Material LoadNewMaterial(string shaderPath)
{
return new Material(Shader.Find(shaderPath));
}
public static Material ResourcesLoadMaterial(string materialPath)
{
return Resources.Load<Material>(materialPath);
}
public static void setMaterial(Material material)
{
material.SetInt("_StraightAlphaInput", 1);
material.SetInt("_STRAIGHT_ALPHA_INPUT", 1);
}
public static TextAsset testLoadAtlasText(string spinePath)
{
TextAsset atlasText = Resources.Load<TextAsset>(spinePath);
return atlasText;
}
public static Texture2D testLoadTexture(string spinePath)
{
Texture2D texture = Resources.Load<Texture2D>(spinePath);
return texture;
}
/// <summary>
/// 生成一个spine
/// </summary>
/// <param name="spinePath"></param>
/// <returns></returns>
public static SkeletonGraphic CreateRuntimeAssetsAndGameObject(string spinePath,Transform transform)
{
//TextAsset skeletonJson,TextAsset atlasText,Texture2D[] textures,Material materialPropertySource
TextAsset skeletonJson = Resources.Load<TextAsset>(spinePath+".skel");
Material materialPropertySource = Resources.Load<Material>(spinePath+"_Material");
//暂时只解析一个texture
Texture2D texture = Resources.Load<Texture2D>(spinePath);
Texture2D[] textures= new Texture2D;
textures = texture;
TextAsset atlasText = Resources.Load<TextAsset>(spinePath+".atlas");
SpineAtlasAsset runtimeAtlasAsset = SpineAtlasAsset.CreateRuntimeInstance(atlasText, textures, materialPropertySource, true);
SkeletonDataAsset runtimeSkeletonDataAsset = SkeletonDataAsset.CreateRuntimeInstance(skeletonJson, runtimeAtlasAsset, true);
runtimeSkeletonDataAsset.GetSkeletonData(false); // preload.
SkeletonGraphic runtimeSkeletonAnimation = SkeletonGraphic.NewSkeletonGraphicGameObject(runtimeSkeletonDataAsset, transform, materialPropertySource);//SkeletonAnimation.NewSkeletonAnimationGameObject(runtimeSkeletonDataAsset);
// Extra Stuff
runtimeSkeletonAnimation.Initialize(false);
/*runtimeSkeletonAnimation.Skeleton.SetSkin("base");
runtimeSkeletonAnimation.Skeleton.SetSlotsToSetupPose();
runtimeSkeletonAnimation.AnimationState.SetAnimation(0, "run", true);
runtimeSkeletonAnimation.GetComponent<MeshRenderer>().sortingOrder = 10;
runtimeSkeletonAnimation.transform.Translate(Vector3.down * 2);*/
return runtimeSkeletonAnimation;
}
}
public class InstanceCoroutine : MonoBehaviour
{
}
public static class CoroutineConfig
{
public static List<Type> LuaCallCSharp
{
get
{
return new List<Type>()
{
typeof(WaitForSeconds),
typeof(WWW)
};
}
}
}
}lua中: local spine2 = CS.UnityEngine.InstanceTool.CreateRuntimeAssetsAndGameObject(animPath, parentObject:GetComponent("Transform"));
print("spine2:" .. tostring(spine2));
spine2.gameObject.name = "spine_" .. "222";
spine2.transform.localScale = { x = 100, y = 100 };
--spine2.Skeleton:SetSkin("animation");
spine2.Skeleton:SetSlotsToSetupPose();
spine2.AnimationState:SetAnimation(0, animName, true);
spine2.transform.localPosition = newPos;
注意这个地方勾上,
spineObject.material:SetInt("_StraightAlphaInput", 1);
页:
[1]