Skip to content

Commit

Permalink
add load custom spine file, tc without pause, init emo location
Browse files Browse the repository at this point in the history
  • Loading branch information
Tualin14 committed Feb 17, 2023
1 parent 5dd36ae commit 8480b17
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
20 changes: 19 additions & 1 deletion Assets/ArisStudio/MainControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ private void Update()
if (isTyping)
{
autoTimer = 0;
isPlaying = false;
return;
}

Expand Down Expand Up @@ -263,6 +262,20 @@ public void PreLoad(string text)
sprFactory.CreateSprGameObjectWithComm(textSplit[2], textSplit[3]);
break;
}
case "custom":
{
var customImgList = text.Split('[')[1].Split(']')[0].Split(',');
sprFactory.CreateCustomGameObjectWithDef(textSplit[2], float.Parse(textSplit[3]), textSplit[4], textSplit[5],
customImgList);
break;
}
case "customC":
{
var customImgList = text.Split('[')[1].Split(']')[0].Split(',');
sprFactory.CreateCustomGameObjectWithComm(textSplit[2], float.Parse(textSplit[3]), textSplit[4], textSplit[5],
customImgList);
break;
}
case "char":
{
var charImgList = text.Split('[')[1].Split(']')[0].Split(',');
Expand Down Expand Up @@ -387,6 +400,11 @@ private void ParseTextIntoCommand(string text)
textArea.SetText(tt[1], tt[3], tt[5]);
break;

case "tc":
tt = text.Split('\'');
textArea.SetText(tt[1], tt[3], tt[5]);
break;

case "th":
sprFactory.TextWithHl(l[1]);
isPlaying = false;
Expand Down
92 changes: 92 additions & 0 deletions Assets/ArisStudio/Spr/SprFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,93 @@ private IEnumerator LoadAndCreateSprGameObject(string nameId, string sprName, Ma
sprGo.SetActive(false);
}

public void CreateCustomGameObjectWithDef(string nameId, float scale, string idle, string customName, string[] imgList)
{
StartCoroutine(LoadAndCreateCustomGameObject(nameId, scale, idle, customName, imgList, defMaterial));
}

public void CreateCustomGameObjectWithComm(string nameId, float scale, string idle, string customName, string[] imgList)
{
StartCoroutine(LoadAndCreateCustomGameObject(nameId, scale, idle, customName, imgList, commMaterial));
}

// ReSharper disable Unity.PerformanceAnalysis
private IEnumerator LoadAndCreateCustomGameObject(string nameId, float scale, string idle, string customName, string[] imgList,
Material stateMaterial)
{
var sprBaseClone = Instantiate(sprBaseGo);
sprBaseClone.name = nameId;
var sprGo = sprBaseClone.transform.Find("SprObject").gameObject;

# region LoadSpr

var sprPath = Path.Combine(sprDataPath, customName);
var atlasPath = $"{sprPath}.atlas";
var skelPath = $"{sprPath}.skel";

string atlasTxt;
using (var uwr = UnityWebRequest.Get(atlasPath))
{
yield return uwr.SendWebRequest();
atlasTxt = uwr.downloadHandler.text;
}

var atlasTextAsset = new TextAsset(atlasTxt);

var textures = new Texture2D[imgList.Length];
for (var i = 0; i < imgList.Length; i++)
{
byte[] imageData;
var imgPath = Path.Combine(sprDataPath, imgList[i].Trim());
using (var uwr = UnityWebRequest.Get(imgPath))
{
yield return uwr.SendWebRequest();
imageData = uwr.downloadHandler.data;
}

var texture = new Texture2D(1, 1);
texture.LoadImage(imageData);
texture.name = Path.GetFileNameWithoutExtension(imgPath);
textures[i] = texture;
}

var sprAtlasAsset = SpineAtlasAsset.CreateRuntimeInstance(atlasTextAsset, textures, stateMaterial, true);

var attachmentLoader = new AtlasAttachmentLoader(sprAtlasAsset.GetAtlas());
var binary = new SkeletonBinary(attachmentLoader);
binary.Scale *= SprScale * scale;

byte[] skelData;
using (var uwr = UnityWebRequest.Get(skelPath))
{
yield return uwr.SendWebRequest();
skelData = uwr.downloadHandler.data;
}

var skeletonData = binary.ReadSkeletonData(customName, skelData);
var stateData = new AnimationStateData(skeletonData);
var sprSkeletonDataAsset = SkeletonDataAsset.CreateSkeletonDataAsset(skeletonData, stateData);

# endregion

var skeletonAnimation = SkeletonAnimation.AddToGameObject(sprGo, sprSkeletonDataAsset);

skeletonAnimation.Initialize(false);
skeletonAnimation.Skeleton.SetSlotsToSetupPose();


skeletonAnimation.AnimationState.SetAnimation(0, idle, true);

sprGo.GetComponent<SprState>().SprInit();
sprGo.GetComponent<OldSprEmotion>().InitEmoticon();

ssList.Add(nameId, sprGo);

debugConsole.PrintLog($"Load Spr: <color=lime>{nameId}</color>");

sprGo.SetActive(false);
}

public void CreateCharGameObjectWithDef(string nameId, float scale, string charFolder, string[] charImgList)
{
StartCoroutine(LoadAndCreateCharacterGameObject(nameId, scale, charFolder, charImgList, defMaterial));
Expand Down Expand Up @@ -268,6 +355,11 @@ private void SCommand(string sCommand)
ssList[l[1]].GetComponent<OldSprEmotion>().PlayEmoticon(l[3]);
break;
}
case "emoInit":
{
ssList[l[1]].GetComponent<OldSprEmotion>().InitEmoticon();
break;
}

// SprAnimation
case "empty":
Expand Down
1 change: 0 additions & 1 deletion ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ GraphicsSettings:
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16003, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 2.2.1
bundleVersion: 2.2.2
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down

0 comments on commit 8480b17

Please sign in to comment.