Unity 6.3
0 онлайн 90 гостей 3 в системе
Вход

Sprite.GetScriptableObjects

Declaration

public uint GetScriptableObjects(ScriptableObject[] scriptableObjects);

Параметры

Параметр Описание
scriptableObjects Массив ScriptableObject, содержащий ScriptableObjects, на который ссылается спрайт.

Возвращаемое значение

УИНТ Возвращает число полученных ScriptableObjects.

Описание

Получает массив ScriptableObject, на который ссылается спрайт.

Если размер массивов, переданных в качестве параметров, меньше числа ScriptableObject, на которые ссылается спрайт, массивы не будут изменены и результаты будут ограничены.

Если размер массивов, переданных в качестве параметров, больше числа ScriptableObject, на которые ссылается спрайт, число элементов, используемых в массиве, будет указано в возвращаемом значении метода.

Ниже приведен пример использования добавления, получения и удаления ссылки ScriptableObjects на спрайт.

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); } } }