本文介绍了Unity 4.3-2D,如何以编程方式将精灵指定给对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个对象,该对象将负责创建和显示不同的Sprite,因此我想以编程方式直接访问资产/Sprite,而不是将Sprite拖放到该对象下的层次结构中.

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?

我还想拥有一种数据结构,其中在游戏开始时加载了一些图像,一个数组或一个字典或类似的东西,因此我可以根据需要显示的图像的某些情况进行更改.但是,自从我不熟悉Unity以来,令我感到困惑的是如何创建一个Sprite,并使用Assets文件夹以编程方式对Sprite进行引用.

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.

编辑进度:

我创建了一个这样的精灵数组:

I've created an array of sprites like this:

public Sprite[] mySprites;

在Unity中,我已在数组中手动​​添加了精灵(我只是将png拖动到了变量数组中),因此实际上在这个对象中,我有一个组件,该组件中包含了所有包含精灵的数组

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

在组件内部,我也这样做了:

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转换失败!Castin从Texture2D到Sprite!"

"PPtr cast failed when dereferencing! Castin from Texture2D to Sprite!"

即使没有强制转换(Sprite),我仍然遇到此错误,顺便说一句,我不知道他为什么要告诉我有关Texture2D的信息,因为一切都被设置为sprite

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可能有些困难.您可能需要在内存中创建一个Texture,填充数据,然后将该纹理写入磁盘.之后,您应该能够使用C#的文件或Unity的 WWW.LoadFromCacheOrDownload来阅读该文件.

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.

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

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

要以编程方式加载您的精灵(虽然不是动态创建的精灵),它们必须位于名称为Resources的文件夹下(这是一个特殊文件夹).

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).

即假设您有一个名为MySprite.png的精灵,位于名为Sprites的文件夹下,该文件夹位于名为Resources的文件夹下.然后,您将像这样加载它:

I.e. suppose you have a sprite named MySprite.png under a folder named Sprites which is under a folder named Resources. You would then load it like this:

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

(注意如何不包括Resources文件夹和sprite的扩展名)

(notice how you do not include the Resources folder and the extension of the sprite)

您可以在在运行时加载资源文档中找到更多详细信息./p>


You can find more details in the Loading Resources at Runtime documentation.

我进行了一些搜索,并从此论坛帖子,通常是在您拥有一种类型的列表,然后将其更改为另一种类型时发生的. IE.如果mySprites在某个时候是GameObject [],并且您链接了一堆Sprites,然后将其更改为Sprite [].而且,因为它专门说了Texture2D,所以我假设您的Sprites(或其中的一些)的导入设置被设置为Texture而不是Sprite:

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. I.e. if mySprites was at some point GameObject[] and you linked a bunch of Sprites, and then changed it to 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:

此外,所有MonoBehaviours已经具有一个名为renderer的字段,该字段的类型为渲染器.我建议您重命名该字段以避免混淆,除非您仅以该示例为例.

Also, all MonoBehaviours have already a field named renderer, which is of type Renderer. I would suggest you rename that field to avoid confusion, unless you only used that as an example.

这篇关于Unity 4.3-2D,如何以编程方式将精灵指定给对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:22