Hi, sorry for my english…
i have this code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ChangeMaterial : MonoBehaviour {
public GameObject Obj3D; //object 3d
public Material[] mats; //the materials
private int index = 0;
public Texture[] itemSlots; //texure of the button
void Start () {
Obj3D.GetComponent<Renderer>().material = mats[index];
}
// Update is called once per frame
public void OnGUI ()
{
//button size
int buttonSizeX = 112;
int buttonSizeY = 56;
// number of pixels between the buttons
int buttonSpacing = 1;
// top-left position of the button grid
int xOffset = Screen.width / 2- 300;
int yOffset = Screen.height - 100;
// number of columns
int numCols = 20;
int numButtons = 20; // Change this to itemSlots.Length
Rect r = new Rect(0, 0, buttonSizeX, buttonSizeY); //create button
for (int i = 0; i < numButtons; i++)
{
r.x = xOffset + (i % numCols) * (buttonSizeX + buttonSpacing); // column position
r.y = yOffset + (int)Mathf.Floor((float)i / numCols) * (buttonSizeY + buttonSpacing); // row position
if (GUI.Button(r,itemSlots[i])) // change i.ToString() to itemSlots[i].GetComponentInChildren().icon
{
index--;
if (index < 0)
{
index = mats.Length - 1;
}
Obj3D.GetComponent<Renderer>().material = mats[index]; //assign materials for each button
}
}
}
}
This code need for create a button for each material. The materials are 20. I have 20 button in horizontal.
I need that this button scrolling in x axis.
I’ m a newbie in scripting.I think that need create a scroll rect in this code for scrolling all button but how and where ?
tk for help!