scriptableObjects | 包含 Sprite 引用的 ScriptableObject 数组。 |
uint 返回检索到的 ScriptableObject 的数量。
检索 Sprite 引用的 ScriptableObject 的数组。
如果作为参数传递的数组大小小于 Sprite 引用的 ScriptableObject 的数量,数组将不会调整大小,结果将受到限制。
如果作为参数传递的数组大小大于 Sprite 引用的 ScriptableObject 的数量,数组中使用的元素数量将由方法的返回值指示。
以下是对 Sprite 添加、获取和移除 ScriptableObject 引用示例用法。
using UnityEngine;
/* * Creates a custom ScriptableObject and attaches it * to a Sprite. The ScriptableObject is then removed after * the first Update loop so that the messages are only printed once. */
// A custom ScriptableObject to hold any custom data. public class MyScriptableObject : ScriptableObject { public string myCustomData; }
public class Sample : MonoBehaviour { Sprite m_Sprite; void Start() { var customData = ScriptableObject.CreateInstance<MyScriptableObject>(); customData.myCustomData = "My Data"; var texture = Texture2D.whiteTexture; m_Sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, texture.width); var spriteRenderer = gameObject.AddComponent<SpriteRenderer>(); m_Sprite.AddScriptableObject(customData); spriteRenderer.sprite = m_Sprite; }
void Update() { var scriptableObjectCount = m_Sprite.GetScriptableObjectsCount(); if (scriptableObjectCount > 0) { var scriptableObjects = new ScriptableObject[scriptableObjectCount]; var retrieveCount = m_Sprite.GetScriptableObjects(scriptableObjects); //This will print 1 since there is 1 ScriptableObject reference. print(retrieveCount); var myScriptableObject = scriptableObjects[0] as MyScriptableObject; // This will print "My Data" print(myScriptableObject.myCustomData);
// Removing the ScriptableObject reference so that the prints // above no longer executes. m_Sprite.RemoveScriptableObjectAt(scriptableObjectCount - 1); } } }