hi,
i created an array for material and assign them in inspector panel, now i have to access the element which element is selected.
How to access that element?
Please help. Thanks
Here is my code for array.
using UnityEngine;
using System.Collections;
public class Raycast : MonoBehaviour
{
public Ray ray;
public RaycastHit hit;
public Texture2D[] diffuseTextures;
public int indexPointer = 0;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonUp(0)){
if(hit.transform.tag == "CubeTag"){
hit.collider.gameObject.renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
indexPointer++;
print (indexPointer);
if (indexPointer >= diffuseTextures.Length) {
indexPointer = 0;
}
}
}
}
}
}
I was using this for UVs animation, maybe it can help you but it’s in unityscript
//vars for the whole sheet
var colCount : int = 4;
var rowCount : int = 4;
//vars for animation
var rowNumber : int = 0; //Zero Indexed
var colNumber : int = 0; //Zero Indexed
var totalCells : int = 4;
var fps : int = 10;
private var offset : Vector2;
//Update
function Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps); }
//SetSpriteAnimation
function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){
// Calculate index
var index : int = Time.time * fps;
// Repeat when exhausting all cells
index = index % totalCells;
// Size of every cell
var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
// split into horizontal and vertical index
var uIndex = index % colCount;
var vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}