Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 roegel · Jan 31, 2021 at 02:40 PM · scripting problembuttonbackgroundvisibilityplacement

Image texture not visible when loaded in script, and button placement wrt image

Hello, in the example below, I have made prefabs for the UI Button and Raw Image, as I didn't find a way to create them otherwise by script. In order to run this example, create a minimal example with only a Canvas and attach the script to the Canvas. This script runs without errors, but I still have two problems:

1) the texture of the image (a photograph) does not appear in the editor; but if I create the RawImage in the editor, I can see it; I guess something must be missing?

2) I have some trouble centering the button and currently the button is not centered. How should I do it better in the script?

Many thanks.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Bug3 : MonoBehaviour
 {
   Canvas canv;
   GameObject prefab_button,prefab_image,play;
 
   public void Start() {
     prefab_button = Resources.Load("Prefabs/Button") as GameObject;
     prefab_image = Resources.Load("Prefabs/RawImage") as GameObject;
     Texture2D raw = Resources.Load<Texture2D>("Images/P1450962.JPG");
     canv = GameObject.Find("Canvas").GetComponent<Canvas>();
 
     // background image
     background=Instantiate(prefab_image,new Vector3(0,0,0),Quaternion.identity);
     background.name="Background";
     background.GetComponent<RectTransform>().SetParent(
                            canv.GetComponent<RectTransform>());
     background.GetComponent<RawImage>().texture=raw;
     background.GetComponent<RectTransform>().position=new Vector3(0,0,0);
     background.GetComponent<RectTransform>().sizeDelta
       =new Vector2(Display.main.systemWidth,Display.main.systemHeight);
     
     // button 1
     play=Instantiate(prefab_button,new Vector3(0,0,0),Quaternion.identity);
     play.name="Play";
     play.GetComponent<RectTransform>().SetParent(
                               background.GetComponent<RectTransform>());
 
     play.GetComponentInChildren<Text>().text = "Play";
     play.GetComponentInChildren<Text>().fontSize=23;
     play.GetComponent<RectTransform>().localScale=new Vector3(1,1,1);
     play.GetComponent<RectTransform>().sizeDelta=new Vector2(200,50);
     play.GetComponent<RectTransform>().position=new Vector3(-100,-10,0);
 
   }
 
 }
Comment
Add comment · Show 1
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 roegel · Jan 31, 2021 at 05:24 PM 0
Share

I managed to load the texture and see it with the following code replacement:

byte[] byteArray = File.ReadAllBytes("Assets/Resources/Images/P1450962.JPG"); Texture2D raw = new Texture2D(2,2); raw.LoadImage(byteArray);

but not with Resources.Load.

0 Replies

· Add your reply
  • Sort: 

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

221 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 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

How to make a button fixed its position on a gameobject model ? 1 Answer

Adding Listeners in for loop with a temporary variable doesn't work 0 Answers

Problem with stopping audio? 1 Answer

Getting errors with Color type 0 Answers

Unity2D Player Sprite not showing up in either Editor/Game Scene 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