- Home /
Player Skins shop Menu
Hello i made a player skins shop menu but but with this code and only 1 image i can combining with the player material, the image is: picture 1 But i like to make public Sprite[] textures; and to add unlimited sprite to my shop menu and to attach the player material like this: picture 2 And here is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ShopMenu : MonoBehaviour {
public GameObject ShopButtonPrefab;
public GameObject ShopButtonContainer;
public Material playerMaterial;
public Text currencyText;
//public Sprite[] textures;
private int[] costs = {0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Use this for initialization
private void Start ()
{
ChangePlayerSkin(GameManager.Instance.currentSkinIndex);
currencyText.text = "X " + GameManager.Instance.currency.ToString();
int textureIndex = 0;
Sprite[] textures = Resources.LoadAll<Sprite>("Player");
foreach (Sprite texture in textures)
{
GameObject container = Instantiate(ShopButtonPrefab) as GameObject;
container.GetComponent<Image>().sprite = texture;
container.transform.SetParent(ShopButtonContainer.transform, false);
int index = textureIndex;
container.GetComponent<Button>().onClick.AddListener(() => ChangePlayerSkin(index));
container.transform.GetChild(0).GetChild(0).GetComponent<Text>().text = costs[index].ToString();
if ((GameManager.Instance.skinAvilability & 1 << index) == 1 << index)
{
container.transform.GetChild(0).gameObject.SetActive(false);
}
textureIndex++;
}
}
private void ChangePlayerSkin(int index)
{
if ((GameManager.Instance.skinAvilability & 1 << index) == 1 <<index)
{
float x = ((int)index % 4) * 0.25f;
float y = ((int)index / 4) * 0.25f;
if (y == 0.0f)
y = 0.25f;
else if (y == 0.25f)
y = 0.5f;
else if (y == 0.5f)
y = 0.25f;
else if (y == 0.75f)
y = 0.0f;
playerMaterial.SetTextureOffset("_MainTex", new Vector2(x, y));
GameManager.Instance.currentSkinIndex = index;
GameManager.Instance.Save();
}
else
{
int cost = costs[index];
if (GameManager.Instance.currency >= cost)
{
GameManager.Instance.currency -= cost;
GameManager.Instance.skinAvilability += 1 << index;
GameManager.Instance.Save();
currencyText.text = "X " + GameManager.Instance.currency.ToString();
if (ShopButtonContainer.transform.childCount > 0)
{
ShopButtonContainer.transform.GetChild(index).GetChild(0).gameObject.SetActive(false);
}
ChangePlayerSkin(index);
}
}
}
}
![alt text][1] ![alt text][2] [1]: /storage/temp/94080-untitled-3.png [2]: /storage/temp/94084-untitled-5.png
Your answer
Follow this Question
Related Questions
More white object model than required 1 Answer
Terrain detail mesh material not working 0 Answers
Problem with Fuse/Mixamo Animations 0 Answers
Child of a player not syncing position with network? 0 Answers