- Home /
How to place Image in World Space canvas by mouseclick position?
Hello.
I want to create strategy game. When player clicks place in plane, I want to image appear at this place.
I have created canvas with world space and attached camera to its attributes. Then I have rotated and placed it so my plane and canvas are in same position.
Question: How to make some image appear in place of canvas where I click with my mouse?
Answer by SuperMasterBlasterLaser · Jan 03, 2015 at 10:41 AM
I have found solution:
RectTransform rectTransform = pointer.GetComponent<RectTransform>();
rectTransform.position = position;
Since my Canvas is World Space, I just needed to set position depending on raycast hit.
Answer by Kiwasi · Nov 29, 2014 at 07:08 AM
Your in luck. I just recorded a tutorial on a very similar concept. In essense you want to create your image as a prefab then Instantiate it at the mouse position.
But you instanciate whole canvas. I have one canvas on my plane and I want to create image on it.
Same principle applies.
$$anonymous$$ake your image a prefab.
Listen for clicks on your plane or canvas.
Instantiate the prefab at the click position.
Set the parent to the canvas.
Have a go at scripting that. If you have trouble post your script here and I can look over it.
$$anonymous$$y code that will instanciate pointer image:
public void drawPlacePoint(Vector3 position)
{
Vector3 pointPosition = new Vector3(position.x, canvas.transform.position.y, position.z);
GameObject pointer = Instantiate(movePlacePointerPrototype, pointPosition, movePlacePointerPrototype.transform.rotation) as GameObject;
pointer.transform.SetParent(canvas.transform, true);
}
$$anonymous$$y GameObject will call this function like that:
drawPlacePoint(hit.point);
hit is place of plane where I have clicked with my mouse.
Your answer
Follow this Question
Related Questions
I Have a Button in Canvas, i Click anywhere except the button and it gets clicked 0 Answers
Draw a Sprite Round 1 Answer
How can I make nested UI Image elements the same transparency? 1 Answer
How would I display a partial image on a canvas in the new UI system? 1 Answer
UI fade with CanvasGroup vs of Image.color.alpha or Text.color.alpha performance 1 Answer