110 lines
2.9 KiB
C#
Raw Normal View History

2025-01-16 11:42:11 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
namespace QFramework
{
internal class MatAction : IAction
{
public System.Action OnFinished { get; set; }
private MatAction()
{
}
private static readonly SimpleObjectPool<MatAction> mPool =
new SimpleObjectPool<MatAction>(() => new MatAction(), null, 10);
string value;
string deviceName;
string matName;
string index;
string mainTexture;
2025-01-16 11:42:11 +08:00
public static MatAction Allocate(Dictionary<string, string> datas, System.Action OnFinished = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.value = datas.ContainsKey("value") ? datas["value"] : "";
retNode.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : "";
retNode.matName = datas.ContainsKey("matName") ? datas["matName"] : "";
retNode.index = datas.ContainsKey("index") ? datas["index"] : "0";
retNode.mainTexture = datas.ContainsKey("mainTexture") ? datas["mainTexture"] : null;
2025-01-16 11:42:11 +08:00
retNode.OnFinished = OnFinished;
return retNode;
}
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
GameObject obj;
if (string.IsNullOrEmpty(deviceName))
{
obj = Utility.FindObj(value);
}
else
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
}
var mesh = obj.GetComponent<Renderer>();
int matIndex = 0;
int.TryParse(index, out matIndex);
if (string.IsNullOrEmpty(matName) == false)
{
mesh.materials[matIndex] = Resources.Load<Material>("Mat/" + matName);
}
if (mainTexture != null)
{
if (mainTexture == "")
{
mesh.materials[matIndex].mainTexture = null;
}
else
{
mesh.materials[matIndex].mainTexture = Resources.Load<Texture>("Mat/" + mainTexture);
}
}
2025-01-16 11:42:11 +08:00
this.Finish();
}
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;
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}