如何使用脚本向玩家素材添加精灵。我有一个播放器的商店菜单,当喜欢选择一个精灵我喜欢添加到播放器材料但我不知道如何做到这一点。我有我的代码但只是这个我不知道如何添加。这是我的代码,我这样做但不起作用,有人可以告诉我如何将playerSprite添加到playermaterial。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopMenuNew : MonoBehaviour
{
public Transform spritePanel;
public Text spriteBuySetText;
public Text goldText;
[SerializeField]
private int[] spriteCost;
private int selectedSpriteIndex;
private int activeSpriteIndex;
// Use this for initialization
void Start ()
{
GameManager.Instance.state.gold = 999;
UpdateGoldText();
InitShop();
OnSpriteSelect(GameManager.Instance.state.activeSprite);
SetSprite(GameManager.Instance.state.activeSprite);
}
// Update is called once per frame
void Update () {
}
private void InitShop()
{
if (spritePanel == null)
Debug.Log("You did not asign the sprite panel in the inspector");
int i = 0;
foreach (Transform t in spritePanel)
{
int currentIndex = i;
Button b = t.GetComponent<Button>();
b.onClick.AddListener(() => OnSpriteSelect(currentIndex));
Image img = t.GetComponent<Image>();
img.sprite = GameManager.Instance.IsSpriteOwned(i) ? GameManager.Instance.playerSprite[currentIndex] : GameManager.Instance.playerSpriteBuy[currentIndex] ;
i++;
}
}
private void SetSprite(int index)
{
activeSpriteIndex = index;
GameManager.Instance.state.activeSprite = index;
GameManager.Instance.playerMaterial = GameManager.Instance.playerSprite[index];
spriteBuySetText.text = "Current";
GameManager.Instance.Save();
}
private void UpdateGoldText()
{
goldText.text = GameManager.Instance.state.gold.ToString();
}
private void OnSpriteSelect(int currentIndex)
{
Debug.Log("Selecting color button :" + currentIndex);
if (selectedSpriteIndex == currentIndex)
return;
selectedSpriteIndex = currentIndex;
if (GameManager.Instance.IsSpriteOwned(currentIndex))
{
if (activeSpriteIndex == currentIndex)
{
spriteBuySetText.text = "Current";
}
else
{
spriteBuySetText.text = "Select";
}
}
else
{
spriteBuySetText.text = "Buy: " + spriteCost[currentIndex].ToString();
}
}
public void OnSpriteBuySet()
{
Debug.Log("Buy Sprite");
if (GameManager.Instance.IsSpriteOwned(selectedSpriteIndex))
{
SetSprite(selectedSpriteIndex);
GameManager.Instance.Save();
}
else
{
if(GameManager.Instance.BuySprite(selectedSpriteIndex,spriteCost[selectedSpriteIndex]))
{
SetSprite(selectedSpriteIndex);
spritePanel.GetChild(selectedSpriteIndex).GetComponent<Image>().sprite = GameManager.Instance.playerSprite[selectedSpriteIndex];
UpdateGoldText();
}
else
{
Debug.Log("Not enough Gold");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { set; get; }
public SaveState state;
public int currentSkinIndex = 0;
public int currency = 0;
public int skinAvilability = 1;
public int coins;
public Material playerMaterial;
public Sprite[] playerSprite;
public Sprite[] playerSpriteBuy;
public Texture[] textures;
private void Awake ()
{
DontDestroyOnLoad(gameObject);
Instance = this;
Load();
}
public void Save()
{
PlayerPrefs.SetString("save",Helper.Serialize<SaveState>(state));
}
public void Load()
{
if (PlayerPrefs.HasKey("save"))
{
state = Helper.Deserialize<SaveState>(PlayerPrefs.GetString("save"));
}
else
{
state = new SaveState();
Save();
Debug.Log("No Save file found, creating a new one!");
}
}
public bool IsSpriteOwned(int index)
{
return (state.spriteOwned & (1 << index)) != 0;
}
public void UnlockSprite(int index)
{
state.spriteOwned |= 1 << index;
}
public bool BuySprite(int index, int cost)
{
if(state.gold >= cost)
{
state.gold -= cost;
UnlockSprite(index);
Save();
return true;
}
else
{
return false;
}
}
public void ResetSave()
{
PlayerPrefs.DeleteKey("save");
}
}
答案 0 :(得分:1)
所以这里的playerMaterial
财产实际上并非......有用。
它基本上是一张3x5索引卡,上面写着一本书的名字,它不是真正的书。所以当你这样做时:
GameManager.Instance.playerMaterial = GameManager.Instance.otherMaterial[index];
您正在删除索引卡上的图书名称并写入新书的标题,然后想知道为什么书架的内容没有改变。它并没有改变,因为你从未真正对书架做任何事情。
您需要更改播放器Renderer
组件的素材属性才能更改。
E.g。类似......
GameManager.Instance.player.getComponent<Renderer>().sharedMaterial = GameManager.Instance.otherMaterial[index];