我觉得有更好的方法。我有一个纹理,我需要调整大小,然后压缩。这两个函数都需要一些处理时间,因此我需要将每个函数放在一个协程中。我得到它的方式是我先调整大小然后压缩,但看起来他们很快就会变得混乱。构造多个已排序的协同程序以逐个触发并将处理过的变量(在本例中为Texture2D)从一个传递到下一个变量的推荐方法是什么?
[Client]
public void PrepareServerData(Texture2D texToSend, string typeToSend)
{
StartCoroutine(DoGetTexToBytes(texToSend));
playerObj.texWidth = texToSend.width;
playerObj.texHeight = texToSend.height;
playerObj.texFormat = texToSend.format;
playerObj.tranX = tran.x;
playerObj.tranY = tran.y;
playerObj.tranZ = tran.z;
playerObj.type = typeToSend;
Player_ID id = GetComponent<Player_ID>();
playerObj.id = id.MakeUniqueIdentity();
playerObj.strength = strengthToSend;
playerObj.hitpoints = hitPointsToSend;
Network_Serializer serialize = GetComponent<Network_Serializer>();
// Send Data from Client to Server as many small sequenced packets
byte[] bytes = serialize.ObjectToByteArray(playerObj);
StartCoroutine(Network_Transmitter.instance.DoSendBytes(0, bytes));
}
IEnumerator DoGetTexToBytes(Texture2D tex)
{
DoResizeTex(tex);
byte[] texBytes = tex.GetRawTextureData(); // convert texture to raw bytes
byte[] compressedTexBytes = lzip.compressBuffer(texBytes, 9); // compress texture byte array
playerObj.texBytes = compressedTexBytes; // set compressed bytes to player object
yield return new WaitForEndOfFrame();
GameObject infoDisplayText = GameObject.Find("InfoDisplay");
infoDisplayText.GetComponent<Text>().text += "Bytes to send : " + playerObj.texBytes.Length + "\n";
}
IEnumerator DoResizeTex(Texture2D tex)
{
tex.ResizePro(1280, 1024);
tex.Apply();
yield return new WaitForEndOfFrame();
}
答案 0 :(得分:2)
如果您需要在DoResizeTex
开始之前完成DoGetTexToBytes
,那么您需要使用DoGetTexToBytes
从yield
内部调用它:
IEnumerator DoGetTexToBytes(Texture2D tex)
{
yield return StartCoroutine(DoResizeTex(tex));
// Stuff done by DoGetTexToBytes after DoResizeTex has finished
}
IEnumerator DoResizeTex(Texture2D tex)
{
tex.ResizePro(1280, 1024);
tex.Apply();
yield return new WaitForEndOfFrame();
}
编辑:不清楚调整后的纹理是否必须由 DoGetTexToBytes
使用,但我想是的。如果是这种情况,则需要使用数组(上面修改的代码添加纹理的引用传递)通过引用传递纹理。基本上,您只使用Texture2D[]
只使用一个元素,texToSend[0]
是您的旧texToSend
。
答案 1 :(得分:2)
为你的协同程序添加一个回调过程。
private IEnumerator MyCoroutine(Action callback){
yield return null;
if (callback !=null){callback();}
}
用法:
void Start(){
StartCoroutine(MyCoroutine(()=>
{
StartCoroutine(OtherCoroutine());
}
}
如果需要,您还可以通过Action返回协程值。
private IEnumerator MyCoroutine(Action<Texture2D> callback){
yield return null;
Texture2D tex = GetTexture();
if (callback !=null){callback(tex);}
}
void Start(){
StartCoroutine(MyCoroutine((texParam)=>
{
StartCoroutine(OtherCoroutine(texParam));
}
}
IEnumerator OtherCoroutine(Texture2D texture){
yield return null;
texture.DoSomething();
}