Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Celestian_GC · Apr 13, 2018 at 05:49 AM · gameobjectcanvasimagecomponent

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges