- Home /
How do you place an image on an object so that the object size is based on pixels per unit and image size?
I need to be able to place a image on an object (for display in game). I won't know the size of the image (it will be provided by the client) and the pixels per unit COULD be adjusted by the user.
I want the object the image is placed on to grow/shrink based on the size of the image and the pixels per unit. So if the image was 3000x3000 and 100 pixels per unit the object would need to be 3000x3000. IF the user changed the pixels per unit from 100 to 10 then the size of the object and image would need to change to 300x300.
Edit: Actually I did the pixels per unit theorizing wrong. It should be: (height/pixels per unit) X (width/pixels per unit) for size of the object the image is placed on. This will ensure that objects on the image (also scaled to pixels per unit) will fit inside a single unit.
Answer by Celestian_GC · Apr 14, 2018 at 03:48 AM
So, I figured out how to do some of this.
One thing I did do was snag a plugin to deal with the runtime browse to file that was super easy to setup and use: https://assetstore.unity.com/packages/tools/gui/file-browser-windows-macos-98716
Created UI->RawImage object.
Add "Box Collider" component to RawImage you just made.
Create MapManager script, attach to RawImage object.
Create UI->button.
Add onclick slot to button.
Drag RawImage object to the onclick slot you just made.
Select LoadFile() for OnClick function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Crosstales.FB; // see LoadFile() function for more details on this plugin
public class MapManager : MonoBehaviour {
GameObject mapBoard; // GameObject->UI->RawImage, child of "Canvas"
BoxCollider boardCollider; // added box collider to ^^RawImage so objects wouldn't fall through if placed on them
float currentImageX; // storing height
float currentImageY; // storing width
RawImage rawImageMap; // Raw Image attached to mapBoard
WWW currentImageSelected; // image user selected in dialog
// Use this for initialization
void Start () {
mapBoard = gameObject;
rawImageMap = mapBoard.GetComponent<RawImage>();
boardCollider = GetComponent<BoxCollider>();
}
// resize the collider to match new size of the image
void SetColliderSize(float x, float y, float z)
{
boardCollider.size = new Vector3(x, y, z);
}
// load file via dialog at runtime
public void LoadFile()
{
/* open dialog and let user select file at runtime
* Can do this with plugin from:
* https://assetstore.unity.com/packages/tools/gui/file-browser-windows-macos-98716
*/
string extensions = ""; // filter *.jpg and *.png?
string path = FileBrowser.OpenSingleFile("Open File", "", extensions);
// end dialog code
Debug.Log("Selected file: " + path);
currentImageSelected = new WWW("file:///"+path);
rawImageMap.texture = currentImageSelected.texture;
rawImageMap.SetNativeSize();
Debug.Log("Image Name=" + rawImageMap.name);
Debug.Log("A X=" + rawImageMap.texture.height);
Debug.Log("A Y=" + rawImageMap.texture.width);
currentImageX = rawImageMap.texture.width;
currentImageY = rawImageMap.texture.height;
SetColliderSize(currentImageX, currentImageY, 1.0f);
}
}
With that you can "Play" and it will show a button, you click, browse to a local file anywhere on your system and select. The RawImage object will grow to accommodate the image height/width as well as the collider around it.
Your answer
Follow this Question
Related Questions
Disabling A Script on a GameObject From a Different Script 2 Answers
InvalidCastException when trying to access child components 2 Answers
How to deal with many buttons in the Inspector 1 Answer
Is it Posable to make you'r own Camera Component and not use the standard Unity Camera Component? 1 Answer
Best Button for UnityGame 2D 1 Answer