找回密码
 立即注册
查看: 826|回复: 1

[脚本] unity lua加载spine代码

[复制链接]
发表于 2021-4-23 19:53 | 显示全部楼层 |阅读模式
c#:
  1. using UnityEngine;
  2. using XLua;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5. using System;
  6. using UnityEngine.Sprites;
  7. using Spine.Unity;

  8. namespace UnityEngine{
  9.     /// <summary>
  10.     /// 自定义一个工具类
  11.     /// </summary>
  12.     public class InstanceTool{

  13.         /// <summary>
  14.         /// 加载Resources文件下制定路径及名字的Sprite图片
  15.         /// </summary>
  16.         public static Sprite ResourcesSprite(string path)
  17.         {  
  18.             return Resources.Load<Sprite>(path);
  19.         }
  20.         /// <summary>
  21.         /// 加载Material lua里没反射
  22.         /// </summary>
  23.         public static Material LoadNewMaterial(string shaderPath)
  24.         {
  25.             return new Material(Shader.Find(shaderPath));
  26.             
  27.         }
  28.         public static Material ResourcesLoadMaterial(string materialPath)
  29.         {
  30.             return Resources.Load<Material>(materialPath);
  31.             
  32.         }
  33.         public static void setMaterial(Material material)
  34.         {
  35.             material.SetInt("_StraightAlphaInput", 1);
  36.             material.SetInt("_STRAIGHT_ALPHA_INPUT", 1);
  37.             
  38.         }

  39.         public static TextAsset testLoadAtlasText(string spinePath)
  40.         {
  41.             
  42.             TextAsset atlasText = Resources.Load<TextAsset>(spinePath);
  43.             return atlasText;
  44.         }
  45.         public static Texture2D testLoadTexture(string spinePath)
  46.         {
  47.             
  48.             Texture2D texture = Resources.Load<Texture2D>(spinePath);
  49.             return texture;
  50.         }

  51.         /// <summary>
  52.         /// 生成一个spine
  53.         /// </summary>
  54.         /// <param name="spinePath"></param>
  55.         /// <returns></returns>
  56.         public static SkeletonGraphic CreateRuntimeAssetsAndGameObject(string spinePath,Transform transform)
  57.         {
  58.             //TextAsset skeletonJson,TextAsset atlasText,Texture2D[] textures,Material materialPropertySource
  59.             TextAsset skeletonJson = Resources.Load<TextAsset>(spinePath+".skel");
  60.             Material materialPropertySource = Resources.Load<Material>(spinePath+"_Material");
  61.             //暂时只解析一个texture
  62.             Texture2D texture = Resources.Load<Texture2D>(spinePath);
  63.             Texture2D[] textures= new Texture2D[1];
  64.             textures[0] = texture;
  65.             TextAsset atlasText = Resources.Load<TextAsset>(spinePath+".atlas");
  66.             SpineAtlasAsset runtimeAtlasAsset = SpineAtlasAsset.CreateRuntimeInstance(atlasText, textures, materialPropertySource, true);
  67.             SkeletonDataAsset runtimeSkeletonDataAsset = SkeletonDataAsset.CreateRuntimeInstance(skeletonJson, runtimeAtlasAsset, true);
  68.             
  69.             runtimeSkeletonDataAsset.GetSkeletonData(false); // preload.
  70.             SkeletonGraphic runtimeSkeletonAnimation = SkeletonGraphic.NewSkeletonGraphicGameObject(runtimeSkeletonDataAsset, transform, materialPropertySource);//SkeletonAnimation.NewSkeletonAnimationGameObject(runtimeSkeletonDataAsset);
  71.             // Extra Stuff
  72.             runtimeSkeletonAnimation.Initialize(false);
  73.             /*runtimeSkeletonAnimation.Skeleton.SetSkin("base");
  74.             runtimeSkeletonAnimation.Skeleton.SetSlotsToSetupPose();
  75.             runtimeSkeletonAnimation.AnimationState.SetAnimation(0, "run", true);
  76.             runtimeSkeletonAnimation.GetComponent<MeshRenderer>().sortingOrder = 10;
  77.             runtimeSkeletonAnimation.transform.Translate(Vector3.down * 2);*/
  78.             return runtimeSkeletonAnimation;
  79.         }
  80.     }

  81.     public class InstanceCoroutine : MonoBehaviour
  82.     {
  83.     }
  84.     public static class CoroutineConfig
  85.     {
  86.         [LuaCallCSharp]
  87.         public static List<Type> LuaCallCSharp
  88.         {
  89.             get
  90.             {
  91.                 return new List<Type>()
  92.                 {
  93.                     typeof(WaitForSeconds),
  94.                     typeof(WWW)
  95.                 };
  96.             }
  97.         }
  98.     }
  99. }
复制代码
lua中:
  1. local spine2 = CS.UnityEngine.InstanceTool.CreateRuntimeAssetsAndGameObject(animPath, parentObject:GetComponent("Transform"));
  2.             print("spine2:" .. tostring(spine2));
  3.             spine2.gameObject.name = "spine_" .. "222";
  4.             spine2.transform.localScale = { x = 100, y = 100 };

  5.             --spine2.Skeleton:SetSkin("animation");
  6.             spine2.Skeleton:SetSlotsToSetupPose();
  7.             spine2.AnimationState:SetAnimation(0, animName, true);
  8.             spine2.transform.localPosition = newPos;
复制代码
 楼主| 发表于 2021-4-23 19:58 | 显示全部楼层


注意这个地方勾上,
            spineObject.material:SetInt("_StraightAlphaInput", 1);

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-29 02:49 , Processed in 0.095177 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表