简体   繁体   English

Unity 4.3 - 2D,如何以编程方式将精灵分配给对象

[英]Unity 4.3 - 2D, how to assign programmatically sprites to an object

I'm trying to create an object that will be responsible of creating and showing different sprites, so I would like to access directly the assets/sprites programmatically instead of drag and drop a sprite in the hierarchy under that object.我正在尝试创建一个负责创建和显示不同精灵的对象,因此我想以编程方式直接访问资产/精灵,而不是在该对象下的层次结构中拖放精灵。

There's a way to programmatically create a new sprite and assign what I have in the assets folder?有没有办法以编程方式创建一个新的精灵并分配我在资产文件夹中的内容?

I also would like to have a sort of data structure with a few images loaded at the start of the game, an array or a dictionary or something like that so I can change based on some situation which images I need to show.我还想要一种数据结构,在游戏开始时加载一些图像、一个数组或一个字典或类似的东西,这样我就可以根据某些情况更改我需要显示的图像。 But what confuses me since I'm new to Unity is how to create a sprite taking the reference to the sprite programmatically using the assets folder.但是让我感到困惑的是,因为我是 Unity 的新手,所以如何使用资产文件夹以编程方式创建一个精灵引用精灵。

edit with progress:有进展的编辑:

I've created an array of sprites like this:我创建了一个像这样的精灵数组:

public Sprite[] mySprites;

in Unity I've added sprites inside the array manually (I just dragged png's inside the variable array) so actually in this Object I have a Component with this array full of sprites在 Unity 中,我手动在数组中添加了精灵(我只是将 png 拖到了变量数组中)所以实际上在这个对象中我有一个组件,这个数组充满了精灵

inside the component I also have done this:在组件内部我也这样做了:

public SpriteRenderer renderer;
renderer = transform.GetComponent<SpriteRenderer>();
renderer.sprite = (Sprite)mySprites [0];

I got no error until I run the game, then the sprite is NOT assigned and I got this:在我运行游戏之前我没有遇到任何错误,然后没有分配精灵,我得到了这个:

"PPtr cast failed when dereferencing! Castin from Texture2D to Sprite!" “解引用时 PPtr 转换失败!从 Texture2D 到 Sprite 的 Castin!”

I got this error even without casting (Sprite) and btw I don't know why he is telling my about Texture2D since verything is setted as sprite即使没有投射(Sprite),我也遇到了这个错误,顺便说一句,我不知道他为什么要告诉我关于 Texture2D 的信息,因为一切都被设置为 sprite

To create a sprite programmatically might be a little too difficult to do.以编程方式创建精灵可能有点太难了。 You'd probably need to create a Texture in memory, fill in the data, and then write that texture on the disk.您可能需要在内存中创建一个纹理,填充数据,然后将该纹理写入磁盘。 After that, you should be able to read that using C#'s File, or Unity's WWW.LoadFromCacheOrDownload .之后,您应该能够使用 C# 的 File 或 Unity 的WWW.LoadFromCacheOrDownload读取该内容

Here is how you would create a texture dynamically: http://answers.unity3d.com/questions/9919/how-do-i-create-a-texture-dynamically-in-unity.html以下是动态创建纹理的方法: http : //answers.unity3d.com/questions/9919/how-do-i-create-a-texture-dynamically-in-unity.html


To load your sprites programmatically (not the ones created dynamically though), they need to be under a folder with the name Resources (which is a special folder ).要以编程方式加载您的精灵(但不是动态创建的精灵),它们需要位于名为 Resources 的文件夹下(这是一个特殊文件夹)。

Ie suppose you have a sprite named MySprite.png under a folder named Sprites which is under a folder named Resources.即假设您在名为 Sprites 的文件夹下有一个名为 MySprite.png 的精灵,该文件夹位于名为 Resources 的文件夹下。 You would then load it like this:然后你会像这样加载它:

renderer.sprite = Resources.Load<Sprite>("Sprites/MySprite");

(notice how you do not include the Resources folder and the extension of the sprite) (注意您如何不包括资源文件夹和精灵的扩展名)

You can find more details in the Loading Resources at Runtime documentation.您可以在Loading Resources at Runtime文档中找到更多详细信息。


PPtr cast failed when dereferencing!取消引用时 PPtr 转换失败!

I did some searching and found out from this forum post that usually that happens when you had a list of one type and then changed it into another type.我做了一些搜索,并从这个论坛帖子中发现,当您拥有一个类型的列表然后将其更改为另一种类型时,通常会发生这种情况。 Ie if mySprites was at some point GameObject[] and you linked a bunch of Sprites, and then changed it to Sprite[].即,如果 mySprites 在某个时间点是 GameObject[] 并且您链接了一堆 Sprite,然后将其更改为 Sprite[]。 And since it specifically says Texture2D, I'm assuming that the import settings of your Sprites (or some of them) are set to Texture instead of Sprite:并且因为它特别提到了 Texture2D,我假设您的 Sprite(或其中一些)的导入设置被设置为 Texture 而不是 Sprite:

精灵导入设置


Also, all MonoBehaviours have already a field named renderer , which is of type Renderer .此外, 所有 MonoBehaviours 已经有一个名为 renderer 的字段,它的类型为Renderer I would suggest you rename that field to avoid confusion, unless you only used that as an example.我建议您重命名该字段以避免混淆,除非您仅将其用作示例。

Here is real code.. will help you这是真正的代码..会帮助你

void SetSprite(UI2DSprite uiSprite, Sprite[] sprites, string strKey)
 {
     foreach (Sprite stexture in sprites)
     {
         if (stexture.name == strKey)
         {
             uiSprite.sprite2D = stexture;
             break;
         }
     }
 }

use it following the way..按照方法使用它..

UI2DSprite[] uiSprites = tmpObject.GetComponentsInChildren<UI2DSprite>();
 Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
 string resName = "icon_favorite_2";

 foreach (UI2DSprite uiSprite in uiSprites)
 {
 if(uiSprite.name = "icon")
  SetSprite(uiSprite, sprites , resName);

 }

Just to throw my tuppence worth in. I had this problem when I changed a SerializedField from Texture2D to Sprite in the code.只是为了投入我的 tuppence。当我在代码中将 SerializedField 从 Texture2D 更改为 Sprite 时,我遇到了这个问题。 The texture was already a sprite in the editor but it needed to be Sprite in the code.纹理在编辑器中已经是一个精灵,但它需要在代码中是精灵。 The field in the editor initially said type mismatch and then displayed the sprite texture again.编辑器中的字段最初表示类型不匹配,然后再次显示精灵纹理。 To cure it I set the field in the editor to none and the reset it to the required sprite.为了解决它,我将编辑器中的字段设置为无并将其重置为所需的精灵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM