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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with setting texture to material on iOS

Discussion in 'UGUI & TextMesh Pro' started by Archie888, May 29, 2015.

  1. Archie888

    Archie888

    Joined:
    May 17, 2014
    Posts:
    41
    Hello everyone,

    This issue is related to the issue that you only seem to be able to display textures in Image and RawImage components on iOS by using the mobile particle materials. Also, the texture needs to be in the material itself, as the components do not seem to be able to use the specified texture as the texture for the rendering of the assigned material. I have this issue with 4.6.0, however if I remember correctly, my tests on 5.0.0 indicated the prevalence of the issue in later builds. Using the default material, along with a texture actually does work. However, the default materials are not mobile-friendly, and thus produce bad performance on mobile. Creating a separate material for each texture is a hassle. I am going around these issues currently by generating a new material in script as a duplicate of the image's material, to which I attach with the texture from the image's texture slot. This way I can have one material per mobile particle blend mode (3 of them in 4.6.0) as assets in my project for the UI Images and RawImages, effectively giving me both the ability to define the desired blend mode, and saving me from creating all the different materials for the different textures. Currently I also cache the separate dynamically created materials for re-use in the cases where the same texture and blend mode are to be used.

    ^^That is one issue. I have posted a bug report about it already. I don't know the status of that. This post is, however, about a related, more specific issue, that I wish to document here.

    This post is perhaps the only documentation available on an issue that I found, when working with Unity 4.6.0 UI, building for iOS. There is a way to set the main texture for a material successfully in script, in order to, for example, to use it in an Image or RawImage component, which works fine during play in editor, but however does not seem to set the material texture (main texture) when running the build on iOS. This way is to use:

    Code (CSharp):
    1. material.SetTexture(0, texture);
    With the zero as the material index, things seem well in-editor, but not in-device.

    Instead, in order to have the intended functionality working on your iOS build, please use:

    Code (CSharp):
    1. material.SetTexture("_MainTex", texture);
    or

    Code (CSharp):
    1. material.MainTexture = texture;
    , of which the latter one seems to appear to me as a form that is likely to have more longevity in the API than the former.

    As the iOS build process takes a long time, it can be a painful process to find such a bug -- or you might give up altogether. It took me one day's work to locate the issue. When you have such issue, there are always a huge number of areas to consider; perhaps you're not getting a component in iOS correctly, perhaps there is something with the texture import settings -- it could literally be anything. Hopefully this post can help someone.


    Cheers and all the best,
    -Archie888
     
    Last edited: May 29, 2015
  2. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    250
    Thanks for posting this, fortunately I'm actually using the material.MainTexture method, so it works.
     
    Archie888 likes this.
  3. Archie888

    Archie888

    Joined:
    May 17, 2014
    Posts:
    41
    Lucky you indeed! I don't exactly know why I was using the zero parameter way in the first place. I guess I was more comfortable in my own coding style with using a method call instead of a property, and further preferred using an integer index over using a string, as the use of strings an IDs tends to raise fears for sudden unknown bugs in the future, in case of deprecations. One would presume that the zero index for a main texture would be something more prevailing. Perhaps properties are something to be trusted more in the Unity API.
     
    Sailendu likes this.