Author Topic: scrollview UIGrid and bad position after scene modification  (Read 7420 times)

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
scrollview UIGrid and bad position after scene modification
« on: April 20, 2016, 10:21:00 AM »
Hello,

I'm new with NGUI and Unity and I don't understand one think.

I have a made a scrollview with a grid inside and everything seems fine.

When I launche my scene in Unity it's ok position are good.
Whene I lauch the game in Unity, I'm starting on another scene and when I switch to my scene with scollview the scrollview position is not at the good position.

You can see in attached picture : Left is after a scene change and right is when I launch directly my scene.

I can give you more information/configuration, just say me what you want.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #1 on: April 20, 2016, 04:57:52 PM »
It's likely related to your cameras. I can see there being some confusing if both your scenes have UIs with their own cameras set to draw the same exact layer.

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #2 on: April 21, 2016, 07:17:00 AM »
Do you have some good tutoriel that explain how camera function with the UI ?

I was thinking that my problem is related to camera but I haven't enough knoledge about to Camera to understand where are my mistakes.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #3 on: April 21, 2016, 07:51:49 PM »
Certain NGUI elements have to cache the camera that draws the layer they're on in order for anchoring to work properly and some other things. This is done by checking a list of cameras and finding the first one that will draw the layer. If you have more than one camera drawing the same layer, which camera will be used is undefined.

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #4 on: April 22, 2016, 03:49:36 AM »
Hello,

Firstly I want to thank you to try to help a poor newbie like me.

I have create a new layer "SceneUI".
I have put my UIRoot and all stuff (camera, scrollview, ...) in this layer.
I have configure my camera to have a culling mask with this layer.

I have check my Scene when I launch the game ans come from aniother Scene and I found only one other camera.
The other camera have a different layer and in the culling mask my layer "SceneUI" is NOT present.
I think this is the good thing.

But my problem still there.

I have put in attachment my camera settings if needed

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #5 on: April 25, 2016, 12:21:40 PM »
What version of Unity are you using, and what code do you use to load your other scene?

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #6 on: April 26, 2016, 02:21:25 AM »
Hello,

I'm using Unity 5.3.4 on Windows 10.

For loading scene I'm using a SceneLoader that I have write.

  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using Oasis;
  5.  
  6. namespace Oasis
  7. {
  8.     public class SceneLoader : MonoBehaviour
  9.     {
  10.         /// <summary>
  11.         /// Instance de SceneLoader
  12.         /// </summary>
  13.         private static SceneLoader _instance = null;
  14.  
  15.         /// <summary>
  16.         /// Obtenir l'instance du SceneLoader
  17.         /// </summary>
  18.         /// <returns>L'instance du SceneLoader</returns>
  19.         private static SceneLoader GetInstance()
  20.         {
  21.             // si on a pas encore d'instance
  22.             if(SceneLoader._instance == null)
  23.             {
  24.                 // on créé le gameObject à partir du prefab
  25.                 GameObject sceneLoaderGo = PrefabManager.Instantiate("Prefabs/Lib/SceneLoader");
  26.  
  27.                 // on met à jour l'instance à partir du composant
  28.                 SceneLoader.SetInstance(sceneLoaderGo.GetComponent<SceneLoader>());
  29.             }
  30.  
  31.             return SceneLoader._instance;
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Fixer l'instance du SceneLoader
  36.         /// </summary>
  37.         /// <param name="sceneLoader">L'instance du SceneLoader</param>
  38.         private static void SetInstance(SceneLoader sceneLoader)
  39.         {
  40.             SceneLoader._instance = sceneLoader;
  41.         }
  42.  
  43.  
  44.         /// <summary>
  45.         /// Demarrer
  46.         /// </summary>
  47.         void Start()
  48.         {
  49.             // il ne faudra pas détruire cette objet car il va passer sur plusieur scene
  50.             DontDestroyOnLoad(this);
  51.         }
  52.  
  53.  
  54.         /// <summary>
  55.         /// méthode utilisé pour lancer la coroutine de chargement de scene.
  56.         /// </summary>
  57.         /// <param name="sceneName">Scene name.</param>
  58.         private void LaunchCoroutineLoadScene(string sceneName)
  59.         {
  60.             StartCoroutine(this.CoroutineLoadScene(sceneName));
  61.         }
  62.  
  63.  
  64.         /// <summary>
  65.         /// Coroutines utilisé pour charger la scene.
  66.         /// </summary>
  67.         /// <param name="sceneName">le nom de la scene a chargé.</param>
  68.         private IEnumerator CoroutineLoadScene(string sceneName)
  69.         {
  70.             // on charge la scene de chargement. on utilise un chargement async car cela permet que la scene precedente soit déchargé aprés que la nouvelle soit chargé
  71.             AsyncOperation loading = SceneManager.LoadSceneAsync("Loading");
  72.  
  73.             // tant que la scene n'est pas chargé
  74.             while(!loading.isDone)
  75.             {
  76.                 // on attend un petit peu
  77.                 yield return new WaitForSeconds(0.1f);
  78.             }
  79.  
  80.             // on charge la scene de maniére asynchrone
  81.             loading = SceneManager.LoadSceneAsync(sceneName);
  82.  
  83.             // tant que la scene n'est pas chargé
  84.             while(!loading.isDone)
  85.             {
  86.                 // on rempli la barre de chargement. on ajoute 0.1 car le loading.progress ne va que jusqu'a 0.9 et ca permet de ne pas avori de barre de chargement vide
  87.                 LoadingBar.Fill(loading.progress + 0.1f);
  88.  
  89.                 // on attend un petit peu
  90.                 yield return new WaitForSeconds(0.1f);
  91.             }
  92.         }
  93.  
  94.         /// <summary>
  95.         /// charge une scene.
  96.         /// </summary>
  97.         /// <param name="sceneName">nom de la scene.</param>
  98.         public static void LoadScene(string sceneName)
  99.         {
  100.             SceneLoader.GetInstance().LaunchCoroutineLoadScene(sceneName);
  101.         }
  102.     }
  103. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #7 on: April 26, 2016, 11:53:27 PM »
Have you tried seeing what happens if you load the scene right away, without doing it asynchronously? I know there are some weird issues with the async scene loading now -- any UI that was marked as dont destroy on load needs to actually be explicitly moved into the other scene before the first one is unloaded, but you aren't unloading anything yourself so it shouldn't be an issue.

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #8 on: April 28, 2016, 03:04:04 AM »
Hello and thanks again to try to help me.

I have try to use a SceneManager.LoadScene to replace all my async load but it change nothing.

I put gameobject in my grid in the Start() of my scrollview. I have notice that my scrollview.resetposition that I'm done at the end of the start doesn't work. But if I done a resetpostion after a short time it put my grid at the good place.

My UIRoot have is fit on width only.
I have a UISprite with an Anchor Unified on UIRoot
I have a scrollview with panel on the sameobject with an Anchor Unified on UISprite
and then I have my grid

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #9 on: April 29, 2016, 12:31:10 PM »
If adding a delay works then you may be getting a conflict with something, such as anchoring. You can just invoke a function that will yield until the end of frame, then update the scroll view's position.

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #10 on: May 06, 2016, 03:50:56 AM »
Hello,

Sorry for the delay to answer but I'm a father since sunday so it take me some time ;D

How can I yield for the end of the frame ?

I have already launch coroutine that yield some time in seconde but I don't know how to wait until the end of the frame.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #11 on: May 07, 2016, 11:28:20 PM »
return yield new WaitForEndOfFrame(); -- consult Unity's docs.

Fluoo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: scrollview UIGrid and bad position after scene modification
« Reply #12 on: May 10, 2016, 07:14:40 AM »
This solution seems to work.

A very lot of Thanks to you !