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 hvad · Jan 16, 2017 at 02:43 PM · unity 5spritecanvas

World canvas sprite transform script

Dear gods of Unity, how do I set the 3d position of a sprite on a world-space canvas with a c# script?

The canvas is for a cockpit display and the sprite and it's position changes depending on the mode. There are too many modes for me to have a different canvas for each sprite and I can't just set the position via the editor.

Ideally, I would like to have multiple sprites with different positions on 1 worldspace canvas. Please can you suggest a way of doing this?

Comment
Add comment · Show 2
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
avatar image RisingDead_xTR · Jan 16, 2017 at 03:06 PM 0
Share

How are you going to change the mode. By a UI or GUIbutton or a keyboard switch? I will suggest you with a script according to this.

avatar image hvad RisingDead_xTR · Jan 17, 2017 at 09:46 PM 0
Share

@RisingDead Via keyboard/joystick switch. The number of sprites would also change depending on the mode.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RisingDead_xTR · Jan 18, 2017 at 02:03 PM

The Idea is to use arrays in order to change modes.
FIRST CREATE AN EMPTY GAMEOBJECT AND ASSIGN THIS CODE TO THE GAMEOBJECT.
STEP: 1 - Create the amount of sprites you need and group them in empty gameobjects. STEP: 2 - Set the size of the array depending on the number of your modes.
STEP: 3 - Enter the gameobjects in the array.
STEP: 4 - Set the noOfModes int. Depending on your number of modes.

  public GameObject[] modes;//Holds all the modes.
     public int noOfModes = 4;//Enter the number of modes you need to have in your game.
     private int modeNumber = 0;//The current mode to active.
     
     // Use this for initialization
     void Start ()
     {
         modes[1].SetActive(false);//In case we have 4 modes we disable the last three modes leaving the first one active. Or modes[0].
         modes[2].SetActive(false);
         modes[3].SetActive(false);
     }
     
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown("space"))//Executes the command when the user pressed SPACEBAR.
         {
             ModeSwitch();
         }
     }
     
     void ModeSwitch ()
     {
         modes[modeNumber].SetActive(false);//Disables the current mode.
         modeNumber += 1;
         if(modeNumber > noOfModes)//If modeNumber exceeds the no. of modes the modeNumber comes back to 0.
         {
             modeNumber = 0;
         }
         modes[modeNumber].SetActive(true);//Enables the new mode.
     }

If it does not work for you let me know. Hope it helps.

Comment
Add comment · Show 3 · 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
avatar image hvad · Jan 19, 2017 at 11:34 AM 0
Share

@RisingDead I was using something similar to this where I would instantiate a GameObject, parent it to the screen, set the position add a spriterenderer and a sprite but unfortunately it was too resource intensive. FPS took a massive hit. I have no problem with the modes, it's just rendering images. Is there a way to do this with images ins$$anonymous$$d of sprites (and without instantiating a Gameobject)? I am modelling a lot of the aircraft systems so there are a lot of calcs done before the sprite is rendered. Also how do I show code like you have?

avatar image RisingDead_xTR hvad · Jan 21, 2017 at 03:49 AM 0
Share

Sorry for the late reply as I was not at home. Images can only be rendered by the canvas not by the Camera. Without instantiating a Gameobject nothing can be done. A sprite or an image cannot exist on it's own beacuse sprite renderers and image renderers are components of a gameobject (like RigidyBody or any script). I will see to the FPS problem you are facing. When you try to add a comment or reply you will see a toolbar appears on the top. One of them has a 01010 icon Press that to show your code.

avatar image hvad RisingDead_xTR · Jan 25, 2017 at 07:20 PM 0
Share

That's great thank you @RisingDead.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Use sprite mesh for filled image 1 Answer

Panel rect transform is wierd 1 Answer

Why does my RenderTexture/Sprite not match the Canvas' Size? 0 Answers

How to add sprites to a canvas via scripting 1 Answer

Rotating a 2D sprite(Ring) in a fixed position 0 Answers


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