C# ndeed您不能使用ref或out将参数传递给迭代器,为此,您需要使用数组(默认情况下,数组按值传递引用,这意味着数组元素上的任何更改都将反映在迭代器的范围之外)来欺骗它。我会用适当的代码修改我的答案。但是只有一种纹理可以返回。为什么是数组?@Everts
C# ndeed您不能使用ref或out将参数传递给迭代器,为此,您需要使用数组(默认情况下,数组按值传递引用,这意味着数组元素上的任何更改都将反映在迭代器的范围之外)来欺骗它。我会用适当的代码修改我的答案。但是只有一种纹理可以返回。为什么是数组?@Everts,c#,unity3d,coroutine,C#,Unity3d,Coroutine,ndeed您不能使用ref或out将参数传递给迭代器,为此,您需要使用数组(默认情况下,数组按值传递引用,这意味着数组元素上的任何更改都将反映在迭代器的范围之外)来欺骗它。我会用适当的代码修改我的答案。但是只有一种纹理可以返回。为什么是数组?@Everts出于某种原因,我认为Texture2D是struct而不是类。事实上,没有理由把变量框起来,因为它已经是引用类型了,我的错。将(再次)更正代码。我只是想指出,如果参数是一个类似于向量3的结构,那么需要将其装箱到一个数组中,以便调用方能够看到更改
ndeed您不能使用
ref
或out
将参数传递给迭代器,为此,您需要使用数组(默认情况下,数组按值传递引用,这意味着数组元素上的任何更改都将反映在迭代器的范围之外)来欺骗它。我会用适当的代码修改我的答案。但是只有一种纹理可以返回。为什么是数组?@Everts出于某种原因,我认为Texture2D
是struct
而不是类。事实上,没有理由把变量框起来,因为它已经是引用类型了,我的错。将(再次)更正代码。我只是想指出,如果参数是一个类似于向量3的结构,那么需要将其装箱到一个数组中,以便调用方能够看到更改。
[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();
}
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();
}
private IEnumerator MyCoroutine(Action callback){
yield return null;
if (callback !=null){callback();}
}
void Start(){
StartCoroutine(MyCoroutine(()=>
{
StartCoroutine(OtherCoroutine());
}
}
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();
}