スクリプトを使用してプレーヤーのマテリアルにスプライトを追加する方法。私はプレーヤーのショップメニューを持っています。スプライトを選択するのが好きなときは、プレーヤーの素材に追加するのが好きですが、これを行う方法はわかりません。私は自分のコードを持っていますが、これを追加する方法はわかりません。これは私がこのようにしたコードですが、誰かが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");
}
}
このコードで材料にスプライトを追加できますか?public Sprite [] otherMaterial; – Toni
@トーニhttps://docs.unity3d.com/ScriptReference/Material-mainTexture.html – Draco18s
しかし、公共のSprite [] – Toni