Author Topic: Setting previews in Prefab Toolbar manually (png files)  (Read 9223 times)

cr4y

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 5
    • View Profile
Setting previews in Prefab Toolbar manually (png files)
« on: August 24, 2014, 06:43:24 AM »
Hi,
For some reason UIPrefabToolbar preview generation is not working well for us. It behaves quite strange (for example we see transparent background in our button previews, while they are not transparent). Maybe we are doing something wrong. Anyway, we done something fun, and maybe it would be wise to add it to NGUI.

We wanted to be able to use png screenshots created by ourselves instead of auto generated ones.
What I have done is adding field with image path to UISnapshotPoint, and told UIPrefabToolbar to use it if not empty.

This can be useful for both Unity Free and Pro users.

Exact code modifications:
UISnapshotPoint:
  public string thumbnailPath = "";
UISnapshotPointEditor:
  (line 33)   NGUIEditorTools.DrawProperty("Path to thumbnail", serializedObject, "thumbnailPath");

UIPrefabTool:
  (top of LoadPreview function:)
  static Texture2D LoadPreview (Item item, string path="")
   {
      if(path == "") path = "Assets/NGUI/Editor/Preview/" + item.prefab.name + ".png";

  (after finding snapshot point:)
  // Try to find the snapshot point script
      if (point == null) point = child.GetComponentInChildren<UISnapshotPoint>();
      // Use snapshot from png if provided
      if (point != null && point.thumbnailPath != "") {
         item.tex = LoadPreview(item, point.thumbnailPath);
         item.dynamicTex = true;
         return;
      }


Please consider adding something like that to next version of NGUI, I'm sure it could be beneficial outside of my team too :-)

Cheers,
Kris

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #1 on: August 24, 2014, 07:41:31 PM »
There is no LoadPreview function in NGUI. There is a GeneratePreview though. This idea seems fine to me, but instead of specifying a string path, I think it makes more sense to have a Texture2D reference in its place. If set, it will be used instead. I'll add that feature.

cr4y

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #2 on: August 25, 2014, 04:51:04 AM »
There is no LoadPreview function in NGUI. There is a GeneratePreview though.
There are both. Right now LoadPreview is being used by GeneratePreview only in free version of Unity. Anyway there is already code for loading thumbnails from files, this is why adding few lines specified in my post is enough.

This idea seems fine to me, but instead of specifying a string path, I think it makes more sense to have a Texture2D reference in its place. If set, it will be used instead. I'll add that feature.
Great idea, thanks in advance!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #3 on: August 26, 2014, 02:06:17 AM »
Nope, there is no "LoadPreview" anywhere. What version of NGUI are you using?

cr4y

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #4 on: August 26, 2014, 07:34:36 AM »
Nope, there is no "LoadPreview" anywhere. What version of NGUI are you using?
Now I see your point :-)
"Load preview" was separate function in 3.6.4 I were using. Now it's merged with GeneratePreview. (Updating NGUI to 3.7.1 didn't solved our issue and we still see option to set thumbnail manually as valuable).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #5 on: August 27, 2014, 03:52:18 AM »
You can currently specify an explicit thumbnail picture in the latest Pro version, so you'll find it in 3.7.2

cr4y

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #6 on: September 14, 2014, 05:53:14 AM »
Unfortunately right now it works up to pressing play button. After going to play mode every prefab in toolbar starts to use automatic mode of snapshot again. After clicking "Update preview" on each prefab it works again.

cr4y

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #7 on: September 14, 2014, 06:29:09 AM »
Quick and dirty workaround in UIPrefabToolbar:

    public void Fix() {
        foreach (var item in mItems) {
            Debug.Log(item.prefab.name);
            GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(item.prefab);
            UISnapshotPoint point = prefab.GetComponentInChildren<UISnapshotPoint>();
            if (prefab == null || point == null) continue;
            RegenerateTexture(item.prefab, point);
            DestroyImmediate(prefab);
        }
        Repaint();
    }

[...]
void OnGUI(){
  if (GUILayout.Button("Fix")) Fix();


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting previews in Prefab Toolbar manually (png files)
« Reply #8 on: September 15, 2014, 04:14:40 AM »
Thanks, looked into this here is a proper fix. Add this function to UIPrefabTool:
  1.         /// <summary>
  2.         /// GetComponentInChildren doesn't work on prefabs.
  3.         /// </summary>
  4.  
  5.         static UISnapshotPoint GetSnapshotPoint (Transform t)
  6.         {
  7.                 UISnapshotPoint point = t.GetComponent<UISnapshotPoint>();
  8.                 if (point != null) return point;
  9.                
  10.                 for (int i = 0, imax = t.childCount; i < imax; ++i)
  11.                 {
  12.                         Transform c = t.GetChild(i);
  13.                         point = GetSnapshotPoint(c);
  14.                         if (point != null) return point;
  15.                 }
  16.                 return null;
  17.         }
...then change the 2nd line of GeneratePreview() function from using GetComponentInChildren to GetSnapshotPoint:
  1. if (point == null) point = GetSnapshotPoint(item.prefab.transform);