- Home /
Make a selection
Hey guys, I am trying to make an application where u have an object that is made of several parts and u can orbit the camera around it. Moreover, its possible to select different parts and change their materials. But I am currently stuck with selection method.. I want to be able to hover over and see some material change like a temporary glow over possible selection part, and if I click, glow becomes permanent with a different color. I cant figure out.. Please help! Thank you!
PS. And maybe you can also advise how to loop through an array of the objects in order to find their materials, to be able to change them after the selection is over. My problem with efficiency :)
using UnityEngine; using System.Collections.Generic;
public class MouseEventHandling : MonoBehaviour {
public GameObject roof;
public GameObject body;
public GameObject veranda;
public GameObject basePart;
public GameObject fence;
public GameObject stairs;
public GameObject door;
public GameObject bladesL;
public GameObject bladesFabric;
public GameObject windowFrame;
public GameObject windowGlass;
public Material roofMat1;
public Material roofMat2;
public Material bodyMat1;
public Material bodyMat2;
public Material verandaMat1;
public Material verandaMat2;
public Material basePartMat;
public Material fenceMat;
public Material stairsMat;
public Material doorMat;
public Material bladesLMat;
public Material bladesFabricMat;
public Material windowFrameMat;
public Material windowGlassMat;
public Material selectionMat;
public Light dirLight;
public Color cameraBG;
public Color lightColor;
public Color beginColor = new Vector4(0, 0, 0, 1);
public Color endColor = new Vector4(1, 1, 0, 1);
public Color beginColor_sel = new Vector4(0, 0, 0, 1);
public Color endColor_sel = new Vector4(0, 0, 1, 1);
public RaycastHit hit;
public float rayDistance = 500.0f;
private bool isHit = false;
public bool isSelected = false;
void Start()
{
roof.renderer.material = roofMat1;
body.renderer.material = bodyMat1;
veranda.renderer.material = verandaMat1;
basePart.renderer.material = basePartMat;
fence.renderer.material = fenceMat;
stairs.renderer.material = stairsMat;
door.renderer.material = doorMat;
bladesL.renderer.material = bladesLMat;
bladesFabric.renderer.material = bladesFabricMat;
windowFrame.renderer.material = windowFrameMat;
windowGlass.renderer.material = windowGlassMat;
}
void Update ()
{
ChangeMaterialOnHit(roof, roofMat1, selectionMat);
ChangeMaterialOnHit(body, bodyMat1, selectionMat);
ChangeMaterialOnHit(veranda, verandaMat1, selectionMat);
ChangeMaterialOnHit(basePart, basePartMat, selectionMat);
ChangeMaterialOnHit(fence, fenceMat, selectionMat);
ChangeMaterialOnHit(stairs, stairsMat, selectionMat);
ChangeMaterialOnHit(door, doorMat, selectionMat);
ChangeMaterialOnHit(bladesL, bladesLMat, selectionMat);
ChangeMaterialOnHit(bladesFabric, bladesFabricMat, selectionMat);
ChangeMaterialOnHit(windowFrame, windowFrameMat, selectionMat);
ChangeMaterialOnHit(windowGlass, windowGlassMat, selectionMat);
}
void ChangeMaterialOnHit(GameObject part, Material originalMat, Material selectionMat)
{
string name = part.name;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(!isSelected)
{
if(Physics.Raycast(ray, out hit, rayDistance) && hit.collider.name == name)
{
part.renderer.material = HoverGlowMaterial(part);
if (Input.GetMouseButtonDown(0))
{
isSelected = true;
}
}
else
{
part.renderer.material = originalMat;
}
}
if (isSelected)
{
part.renderer.material = SelectedGlowMaterial(part);
}
Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.red);
}
Material HoverGlowMaterial(GameObject target)
{
float lerp = Mathf.PingPong(Time.time, 0.5f);
Color newColor = Color.Lerp(beginColor, endColor, lerp);
selectionMat.SetColor("_GlowColor", newColor);
selectionMat.SetFloat("_GlowStrength", 1.5f);
selectionMat.SetTexture("_MainTex", target.renderer.material.GetTexture("_MainTex"));
return selectionMat;
}
Material SelectedGlowMaterial(GameObject target)
{
float lerp_sel = Mathf.PingPong(Time.time, 0.75f);
Color newColor = Color.Lerp(beginColor_sel, endColor_sel, lerp_sel);
selectionMat.SetColor("_GlowColor", newColor);
selectionMat.SetFloat("_GlowStrength", 0.5f);
selectionMat.SetTexture("_MainTex", target.renderer.material.GetTexture("_MainTex"));
return selectionMat;
}
}