115 lines
3.2 KiB
C#
Raw Normal View History

using QFramework.Example;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using static UnityEditor.Progress;
namespace QFramework
{
internal class LineAction : IAction
{
ResLoader loader;
public System.Action OnFinished { get; set; }
private LineAction()
{
}
private static readonly SimpleObjectPool<LineAction> mPool =
new SimpleObjectPool<LineAction>(() => new LineAction(), null, 10);
public string paths;
public string name;
Color color = Color.green;
float width = 0.1f;
Vector2 scale = Vector2.one;
public static LineAction Allocate(string name, string paths, string color, string width, string lineScale, System.Action OnFinished = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.paths = paths;
retNode.name = name;
if (string.IsNullOrEmpty(color) == false)
{
retNode.color = Utility.ToColor(color);
}
float.TryParse(width, out retNode.width);
if (string.IsNullOrEmpty(lineScale) == false)
{
retNode.scale = Utility.GetVector2FromStrArray(lineScale);
}
retNode.OnFinished = OnFinished;
return retNode;
}
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
loader = ResLoader.Allocate();
loader.Add2Load(QAssetBundle.Line_prefab.LINE, (success, res) =>
{
if (success)
{
GameObject obj = GameObject.Instantiate(res.Asset.As<GameObject>());
obj.name = name;
var render = obj.GetOrAddComponent<LineRenderer>();
render.startWidth = width;
render.endWidth = width;
render.material.color = color;
render.textureScale = scale;
var pathsArr = paths.Split('|');
for (int i = 0; i < pathsArr.Length; i++)
{
Vector3 point = Utility.GetVector3FromStrArray(pathsArr[i]);
render.positionCount += 1;
render.SetPosition(i, point);
}
obj.GetOrAddComponent<LineRendererToMesh>().BakeMesh();
}
this.Finish();
});
loader.LoadAsync();
}
public void OnExecute(float dt)
{
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
OnFinished = null;
Deinited = true;
loader.Recycle2Cache();
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}