Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Image documentation no help...

Discussion in 'Documentation' started by Ramcat, Jan 26, 2015.

  1. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    So I'm trying to do a simple task. I have an Image in a prefab whose sprite is not set. I want to set that dynamically in code. I go to this page:
    http://docs.unity3d.com/ScriptReference/UI.Image-sprite.html
    and it is no help.

    There is no code example of how set that value. This is what documentation is supposed to teach people. It would tell me so many things, how to pull an asset off my hard drive (I know I would only reference it, and at build time the compiler would do the real work), how to assign that value, what data type it takes, and there should be a "here's the way we want you to do that example" as well as "here's the quick and dirty way" example. But all I find is:

    "UI.Image.sprite
    Switch to Manual
    public var sprite: Sprite;
    public Sprite sprite;
    public sprite as Sprite
    Description
    The sprite that is used to render this image.
    Copyright © 2014 Unity Technologies"

    Teaches me nothing, documents nothing.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Indeed and there are boat loads of these kind of issues. We can make the doc team much bigger, and involve the developers more, or, what I am trying to get happen, is to have the documentation available to the community so they can share code examples and help document things which are incomplete. I hope this happens this year.
     
  3. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    So I know there are a lot of extraneous concepts in this code segment - but it's at least something.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     [SerializeField] Image _Map;
    9.     [SerializeField] Sprite _Locked;
    10.     [SerializeField] Sprite _Cleared;
    11.     [SerializeField] Sprite _Active;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {  
    16.         // Retrieve list of arenas
    17.         List<Arena> ArenaList = DataFacade.GetArenaList();
    18.         // Instantiate and position them
    19.         foreach(Arena aArena in ArenaList)
    20.         {
    21.             aArena.GameObject = (GameObject) Instantiate(
    22.              Factory.Instance.GetObject(Objects.ARENAPOINT), Vector3.zero,
    23.              Quaternion.identity);
    24.             aArena.GameObject.transform.SetParent(_Map.transform);
    25.             // Arena position is applied as an "offset" of map position
    26.             aArena.GameObject.transform.position = new Vector2(
    27.              _Map.transform.position.x -  aArena.X,
    28.              _Map.transform.position.y - aArena.Y);
    29.            
    30.             // This is what we want to show in this example -- that the sprite is
    31.             // being set to the "public" variable that must have been
    32.             // pre-populated by the editor before running this code. We call this
    33.             // concept the "editor hole", meaning it is the "hole" you can use to
    34.             // put prefabs from the editor into your code for instantiation.
    35.            
    36.             // Set the progress icon
    37.             Image aImage = aArena.GameObject.GetComponentInChildren<Image>();
    38.             if (GameManager.Instance.User.ArenaNumber > aArena.ID)
    39.             {
    40.                 // Clear
    41.                 aImage.sprite = _Cleared;
    42.             }
    43.             else if (GameManager.Instance.User.ArenaNumber < aArena.ID)
    44.             {
    45.                 // Locked
    46.                 aImage.sprite = _Locked;
    47.             }
    48.             else
    49.             {
    50.                 // Active
    51.                 aImage.sprite = _Active;
    52.             }
    53.         }
    54.     }
    55. }
    The code does not explain what you have to do in the editor to prepare the Sprite and a link should be provided to those instructions.

    Personally I don't like this code - I'd prefer if there was a method to reference the source sprite on my hard drive (in the project space) and at build time the compiler would correct that reference to where that sprite would be on the target machine at run time.

    And how do you set that sprite from dynamically downloaded content??? All this needs to be documented or have links from this point to where those concepts are explained.
     
  4. feiting

    feiting

    Joined:
    Oct 26, 2012
    Posts:
    33
    1) Import your sprite images into a folder called Resources, literally name it that. Unity searches that folder for dynamic assets, even though you don't need it for pre-applied assets (like the prefabs, which you're not doing, hehe)
    2) Load the Resource with this: yoursprite = Resources.Load( Path_and_name_of_file.jpg, typeof(Sprite) ) as Sprite;
    3) gameObject's spriteRenderer.sprite = yoursprite;
    Edit, the path is relative to Resources. So if you have Resources/Folder1/image.jpg then your path_and_file is Folder1/image.jpg, forward slash is used no matter what. I'm on windows, for example, and it works that way.

    Hope that helps. Graham, I do like your post, but the reason I didn't hit "like" is bc you're the Unity dude, but I'm the one answering! Humor aside, I believe we have the same goals, I'm willing to work with you on getting that going after my stuff is finished from the other post
     
    Last edited: Jan 30, 2015
    androidideas likes this.
  5. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Unfortunately the "Resources" folder has its own drawbacks. It moves those assets to every build so Windows, IOS, and Android all get the same resources. To avoid that you have to build three projects, and put Windows assets in one, IOS in another, and Android in the last.

    But I see you mentioned dynamic resources. How are you loading resources to that folder at run-time?

    And I am doing prefabs (see code above) my Factory.GetObject method returns a prefab whose Sprite is not set.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Don't use Resources for sprites, since it prevents auto-atlasing. There is no Resources folder in a build; everything is compiled into an asset file. For loading external assets at runtime you need to use System.IO.File functions (or the WWW class using "file://").

    --Eric
     
  7. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    59
    So why is there not a user-maintainable officially sanctioned wiki? That's what most companies do when they have an API and no will to create proper documentation for it. Create a wiki, stub it with the existing documentation, and open the doors.

    Here's an example of what a properly documented function should look like:
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx

    Versus what I can only describe as obviously code-generated documentation:
    http://docs.unity3d.com/ScriptReference/Serialization.SerializedStateReader.html

    At work I'm firmly against code-generated documentation. Case in point?
    https://developer.gnome.org/gtkmm/stable/classGtk_1_1Container.html

    Unity is a development environment. The API is ~the~ product you are selling, the editor being the tool to work with the APIs in an easier format. I would imagine properly documenting the API would be one of the first things you would do:

    Step 1) Write the user story for the function/feature
    Step 2a) Have a document writer write documentation based on the requirements
    Step 2b) Have a development team implement the feature
    Step 3) Test the feature against the generated document and requirements
    Step 4) Release the feature along with the document.

    I'm imagining it's more like:
    Step 1) Developer gets random feature from the sprint.
    Step 2) Developer implements the feature into dev
    Step 3) QA tests and certifies the feature
    (and then at SOME point in the future)
    The documentation guy goes in and adds documentation for the function.

    The simple fact that you have completely empty documentation pages shows there is no QA for your documents. You generate the documentation from code or a data file and put it out there.

    This isn't really a rant to unity so much as a rant to many APIs out there that seem to throw developers under the bus and rely on the "community" (aka the customers) to pick up the slack.
     
    Last edited: Feb 1, 2015
    sootie8 likes this.
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I think you missed Graham's post above:

     
  9. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    59
    I didn't, "can" and "hope" are not the same as "will" and "am confident". In fact, those words instantly tell me that there is more to what he is saying, which is why his phrasing has a very political and pessimistic tonality to it. I get that they ~want~ to do it, I mean who wouldn't? I just have a feeling it's not going to get done due to some higher up or external influence, hence my rant post :)
     
  10. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Um, I have wanted a system to allow customers to contribute for many years. I'm certain it will happen, but it hasn't happened in the last 18 months when the internal team who will do this for us took it over. It's just stuck in a queue of work.
     
  11. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    59
    I get it, I really do. I work as a software engineer for a company that lists Walmart and Target as clients so I get the whole "priorities" thing. Documentation simply doesn't come across as "progress" for large/important clients. I honestly don't mind starting up a documentation project as I really love Unity and I'd hate to loose users to Unreal Engine or other systems due to poor documentation. Do you think there's any way the unity team could provide some sort of serializable (xml, json, binary, etc) dump of the documentation? If so, I could use it to seed a documentation site and then start adding valuable information from there? Maybe even a css/image kit to match the unity site look and feel? I'd use bootstrap as a base as responsive/mobile support would be a necessity.

    If so ping me, I can throw up an azure instance and whip up an MVC site pretty quick. I know you guys use PHP (which explains the 2600 lines of html that comes back on this page :p), but if I'm going to do a site, I'd like to do it right.

    And if there's anyone else out there who'd like to help set it up, let me know as well. I can't possibly fix the documentation issues by myself.
     
    sootie8 likes this.
  12. feiting

    feiting

    Joined:
    Oct 26, 2012
    Posts:
    33
    So like, I couldn't get any of my code-determined images to load in my cards until I found the Resources thing after literally 5 hours of searching. So, thanks for the advice guys, and now I'm mostly clueless as to which method I should use. I'll reread your posts on using the system IO and such
     
  13. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
    You should go for it. I myself am teaching a junior developer, trying to tell him to always use the docs as the first port of call. Followed by him returning to me, having found a half baked attempt at an explanation of some function with the bare minimum in terms of code examples provided.

    Anyway, you should your own thread for this, so people can find it, I for one would be happy to chip in with some of the obscure editor classes at least :)