【问题标题】:How to get a texture that changing every frames on a game object?如何获得改变游戏对象上每一帧的纹理?
【发布时间】:2017-09-11 17:28:00
【问题描述】:

首先,对不起我的英语。 我想将一些纹理从每个帧都有不同纹理的游戏对象中备份到一个列表中。我尝试这样做,并且每帧都将纹理推送到列表中,但是所有推送的纹理似乎都被覆盖到了最新的纹理……我也尝试了 Instantiate();当每个纹理被推入列表但纹理完全透明时。我没有使用电影纹理。有谁知道怎么做这个??

我可能解释得不够清楚,所以如果有什么不清楚的地方请问我。 谢谢

//Those line of codes are looping every frame //This is the current code transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture); //This is the another code that I tried transitionTextures.Add(Instantiate(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture));

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    **transitionTextures.Add(targetGameObject.GetComponent<Renderer>().materials[0].mainTexture);** 
    

    这行代码将 targetGameObject.GetComponent().materials[0].mainTexture 的引用复制到列表中,因此根据引用复制规则,当 targetGameObject 的 mainTexture 发生更改时,它会将列表中的所有引用更改为更新的新的价值。所以你应该创建一个新对象,所以它应该像这样按值复制

     Texture2D temp = targetGameObject.GetComponent<Renderer>().materials[0].mainTexture as Texture2D;
    
     Texture2D tex = new Texture2D(temp.width, temp.height);
     tex.SetPixels(temp.GetPixels());
     tex.Apply();
    
     transitionTextures.Add(tex);
    

    试试这个,也许对你有帮助。

    【讨论】:

    • 感谢您的回答!我尝试了这段代码,实际上它还有另一个问题...... temp.GetPixels() 返回“纹理'无数据”的错误。我花了几个小时,但还没有运气。你知道为什么会这样吗?
    猜你喜欢
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多