我有一個ImageSequencePlayer
腳本,它具有字符串輸入以選擇從哪裏加載圖像。圖像的Sprite[]
數組現在是靜態的,但我仍然將它們多次加載到相同的數組中。我有更多使用此工具的預製件,例如一個角色,敵人,一些陷阱,火焰等等。如何使用Singletons
創建一個體面的設計?使它一旦prefab
使用這個腳本有一個不同的位置,遊戲管理器尋找它並加載精靈,然後給所有需要的?如果不是這樣的話怎麼樣?使用單例爲單位設計初始化
using UnityEngine;
using System.Collections;
public class ImageSequencePlayer : MonoBehaviour
{
private int currentImageIndex;
private SpriteRenderer spriteRenderer;
private static Sprite[] spriteSheet;
[Tooltip("The location of your main spritesheet.")]
public string spritesLocation;
public float frameRate = 24.0f;
public enum PlayMode
{
order, random, uponNeeded
}
public PlayMode playMode = PlayMode.order;
private bool updateEnabled = true;
[Tooltip("Decides if the random playback can iterate. If no, it doesn't play, it is frozen.")]
public bool canIterate = true; //if the random playback can iterate. if no, it doesn't play.
[Tooltip("Is there some non looping animation before the loop? If so, right after it's done, it starts looping.")]
public bool warmUp = false;
[Tooltip("If you have a warmup sheet location, put it here.")]
public string warmUpLocation = "";
private static Sprite[] warmUpSprites;
void Start()
{
try
{
spriteSheet = null;
spriteSheet = Resources.LoadAll<Sprite>(spritesLocation);
}
catch(MissingSpriteSheetException ex)
{
Debug.Log(ex.Message);
}
try
{
spriteRenderer = GetComponent<SpriteRenderer>();
}catch
{
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
}
if(warmUp)
{
switch (this.gameObject.tag)
{
case "candleLight":
warmUpSprites = null;
warmUpSprites = Resources.LoadAll<Sprite>("warmUpFlames");
break;
default:
warmUpSprites = null;
warmUpSprites = Resources.LoadAll<Sprite>(warmUpLocation);
break;
}
//meaning we do have something to warm up
}else
{
//so if we did want some warmup, we load it,
//if we didn't, we just free the memory.
warmUpSprites = null;
warmUpLocation = null;
}
if(playMode == PlayMode.uponNeeded)
{
//if we need it occasionally, we just disable everything else.
warmUp = false;
updateEnabled = false;
warmUpLocation = null;
warmUpSprites = null;
}
}
void LateUpdate()
{
if (warmUp)
{
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % warmUpSprites.Length;
spriteRenderer.sprite = warmUpSprites[currentImageIndex];
if(currentImageIndex >= warmUpSprites.Length-1)
{
currentImageIndex = 0;
warmUp = false;
//now easing on the memory, not another warmup will happen of course:
warmUpLocation = null;
warmUpSprites = null;
}
}
if (updateEnabled && !warmUp)
{
switch (playMode)
{
case PlayMode.order:
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % spriteSheet.Length;
spriteRenderer.sprite = spriteSheet[currentImageIndex];
break;
case PlayMode.random:
updateEnabled = false;
StartCoroutine(RandomizedPlay());
break;
}
}
}
IEnumerator RandomizedPlay()
{
int oldIndex = 0;
int currentIndex;
int iterNumber = 0;
while (canIterate)
{
currentIndex = Random.Range(0, spriteSheet.Length - 1);
if(currentIndex == oldIndex)
{
while((currentIndex == oldIndex) && (iterNumber < 8))
{
currentIndex = Random.Range(0, spriteSheet.Length - 1);
iterNumber++;
}
}
spriteRenderer.sprite = spriteSheet[currentIndex];
oldIndex = currentIndex;
iterNumber = 0;
yield return new WaitForSeconds(1.0f/frameRate);
}
}
public void OccasionalAnimation(bool backWardsNeeded)
{
StartCoroutine(OccasionalAnimEnum(backWardsNeeded));
}
public IEnumerator OccasionalAnimEnum(bool backWardsNeeded)
{
currentImageIndex = 0;
while (currentImageIndex < spriteSheet.Length)
{
//meaning while we do have anything to play
spriteRenderer.sprite = spriteSheet[currentImageIndex];
currentImageIndex++;
yield return new WaitForSeconds(1.0f/frameRate);
}
if (backWardsNeeded)
{
//so we need to play the shit backwards as well, like in the book
currentImageIndex = spriteSheet.Length - 2;//so we won't repeat the last again.
while (currentImageIndex >= 0)
{
//meaning while we do have anything to play
spriteRenderer.sprite = spriteSheet[currentImageIndex];
currentImageIndex--;
yield return new WaitForSeconds(1.0f/frameRate);
}
}
//at the end it should be at the starting sprite.
currentImageIndex = 0;
}
}
很高興看到你不急於這次,並願意改變你的設計。你可以解釋一下這個問題:**這樣做,只要使用這個腳本的預製具有不同的位置,遊戲管理器就會查找它並加載這些小精靈,然後給出所有需要這些的**。並請添加您當前的代碼以及 –
是的,我看到了原因,並意識到繼續錯誤的設計將花費更多的時間比重寫它。 我在一秒鐘內包含了圖像序列播放器。 – agiro
所以這個想法是,我有腳本,使用一個靜態的spriteSheet數組加載的精靈一個接一個地播放。 如何知道從哪裏加載圖像?我在編輯器中鍵入文件夾的名稱,在那裏只有必要的圖像。 就像你之前說的那樣,這些圖像會被加載很多次,但現在已經加載到一個數組中。 爲了解決這個問題,我想將這些字符串值加載到字符串數組中,並讓Manager腳本知道它必須從這些數組中加載。 – agiro