- Home /
Select an object, then change the texture
Hi,
I have a number of different objects and a number of textures which people can apply to those objects.
So what I would like; someone clicks (in this case, touches, since I'm using Qualcomm AR) an object, after which they can select one of the textures to apply to that object.
This works fine when I display a set of texture triggers for each of the objects seperately, but I would like to have just one set of textures which can be applied to each one of the objects.
Attached is a Mondriaan-like illustration of what I am trying to achieve.
Currently, I have these lines of code;
var Object01 : GameObject;
Object01 = GameObject.Find("Object01");
var Object02 : GameObject;
Object02 = GameObject.Find("Object02");
function Start () {
}
function Update () {
var hit : RaycastHit;
var touches = Input.touches;
for (var touch : Touch in touches){
var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
if (Physics.Raycast (ray,hit,Mathf.Infinity)){
// Object01 Texture Triggers
if(hit.collider.name == ("Object01White")&& touch.phase == TouchPhase.Began){
Object01.renderer.material.mainTexture = null;
Object01.renderer.material.color = Color.white;
}
if(hit.collider.name == ("Object01Black")&& touch.phase == TouchPhase.Began){
Object01.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
Object01.renderer.material.mainTextureScale = Vector2 (6,6);
}
if(hit.collider.name == ("Object01Red")&& touch.phase == TouchPhase.Began){
Object01.renderer.material.mainTexture = Resources.Load("bright_red");
Object01.renderer.material.mainTextureScale = Vector2 (8,8);
}
// Object02 Texture Triggers
if(hit.collider.name == ("Object02White")&& touch.phase == TouchPhase.Began){
Object02.renderer.material.mainTexture = null;
Object02.renderer.material.color = Color.white;
}
if(hit.collider.name == ("Object02Black")&& touch.phase == TouchPhase.Began){
Object02.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
Object02.renderer.material.mainTextureScale = Vector2 (6,6);
}
if(hit.collider.name == ("Object02Red")&& touch.phase == TouchPhase.Began){
Object02.renderer.material.mainTexture = Resources.Load("bright_red");
Object02.renderer.material.mainTextureScale = Vector2 (8,8);
}
}
}
}
As you can see, this will result in -a lot- of redundant code.
My best guess would be to replace Object01/Object02/etc with something generic which is generated by clicking one of the objects.
I think I have the pieces, now I'm just wondering how they would fit together.
Any help would be very much appreciated.
[1]: /storage/temp/6379-unitytextureswitch.png
Answer by jpvanmuijen · Jan 04, 2013 at 07:29 PM
Got it!
// Define objects and selected object
var Object01: GameObject;
Object01 = GameObject.Find("Object01");
var Object02 : GameObject;
Object02 = GameObject.Find("Object02");
var selectedObject: GameObject;
function Start () {
}
function Update () {
var hit : RaycastHit;
var touches = Input.touches;
for (var touch : Touch in touches){
var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
if (Physics.Raycast (ray,hit,Mathf.Infinity)){
// Find out what object is hit and store in selectedObject
if(hit.collider.name == ("Object01")&& touch.phase == TouchPhase.Began){
selectedObject= GameObject.Find("Object01");
}
if(hit.collider.name == ("Object02")&& touch.phase == TouchPhase.Began){
selectedObject = GameObject.Find("Object02");
}
// Set texture for the selected object
if(hit.collider.name == ("TextureWhite")&& touch.phase == TouchPhase.Began){
selectedObject.renderer.material.mainTexture = null;
selectedObject.renderer.material.color = Color.white;
}
if(hit.collider.name == ("TextureBlack")&& touch.phase == TouchPhase.Began){
selectedObject.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
selectedObject.renderer.material.mainTextureScale = Vector2 (6,6);
}
}
}
}
Not sure if it's the most elegant or efficient way, but it works!
Thanks a lot for the pointer Vonni.
Answer by Vonni · Jan 04, 2013 at 04:02 PM
Not real code
Raycast on touch
Set object hit to a variable var selectedObject : GameObject;
On click texture buttons: selectedObject.renderer.material.mainTexture = Resources.Load("bright_red"); selectedObject.renderer.material.mainTextureScale = Vector2 (8,8);
Thanks, that actually makes sense.
I'm just struggling to set the object hit to a variable i.e. where it should go (sorry, coding newbie).
This is what I've changed it to;
function Start () {
}
function Update () {
var hit : RaycastHit;
var touches = Input.touches;
for (var touch : Touch in touches){
var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
if (Physics.Raycast (ray,hit,$$anonymous$$athf.Infinity)){
var selectedObject : GameObject;
if(hit.collider.name == ("TextureWhite")&& touch.phase == TouchPhase.Began){
selectedObject.renderer.material.mainTexture = null;
selectedObject.renderer.material.color = Color.white;
}
if(hit.collider.name == ("TextureBlack")&& touch.phase == TouchPhase.Began){
selectedObject.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
selectedObject.renderer.material.mainTextureScale = Vector2 (6,6);
}
}
}
}
Do I still need the variables at the beginning of my previous code?
Also, the texture switches are objects themselves, how do I make sure they don't change their own texture (since that's the last object stored in selectedObject)?
Just another little nudge and I should be fine, thanks again!
Answer by hollym16 · Dec 17, 2014 at 02:02 PM
Hey, I'm doing something similar to this…. well actually the opposite. Where you have selected the object then set the colour, I want to select the colour then apply it to an object. Any ideas on how to achieve this? You're code was a great starting point :)
(Also I know this isn't an answer but I know notifications are a bit weird for this, just wanted to make sure it s seen)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trying to use a basic combat system 2 Answers
GUI button's disappearing behind full screen Texture 1 Answer
Converting Autowaypoint (JS) to C# 1 Answer
Can't save script 1 Answer