- Home /
Texture following cursor...
A simple question, how can I, when a button is clicked, instantiate a texture with the cursor as the center point (empty GameObject to define center, maybe?) and make it follow the cursor? I thought of having an empty follow the cursor and add the texture as a child, but that will not be so organized, and therefore I ask of you any simpler means of executing what I desire.
Thanks in advance!
[Note, this is not a request for a script, that is what the forums are for. But I ask of you, please, if you post any code that helps me, keep it in C#, if you can, to make life easier for me. Thank you.]
Answer by clunk47 · Aug 16, 2013 at 08:23 PM
I may be wrong, but it seems like you're wanting to click a button with a certain texture, and set that as the cursor? Try this out. You will attach this to your main camera, or some empty game object. You will need to assign the three textures in the inspector. This will create 3 buttons on the left side of the screen. Each button will show a texture that you have assigned. When you click each button, the image from the gui content will appear beneath the cursor as if you're dragging it. Hope I addressed your question appropriately.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
Texture currentCursor;
public Texture sel1;
public Texture sel2;
public Texture sel3;
int w = 64;
int h = 64;
void OnGUI()
{
if(GUI.Button(new Rect(0, 0, w, h), sel1))
{
currentCursor = sel1;
}
if(GUI.Button (new Rect(0, 66, w, h), sel2))
{
currentCursor = sel2;
}
if(GUI.Button(new Rect(0, 132, w, h), sel3))
{
currentCursor = sel3;
}
if(currentCursor != null)
{
GUI.DrawTexture(new Rect(Input.mousePosition.x - (w / 2), Screen.height - Input.mousePosition.y - (h / 2), w, h), currentCursor);
}
}
}
That is kind of what I intended in the first place, but what I need is something like a button with an icon, but when you click on that button, a texture is selected, that can be placed down where the mouse is clicked. So it is kind of like selecting the texture by pressing its button, then being able to place the texture down... Tell me if that makes sense, and I can rephrase if needed.
Take a look at WorldtoScreenPoint and its inverse, I think that's what you are after.
Your answer
Follow this Question
Related Questions
Changing a GUI texture while hovering over an object? 2 Answers
my gun wont follow my mouse 0 Answers
How to stop gradient banding? 2 Answers
Assigning UV Map to model at runtime 0 Answers