Author Topic: How to use Resources.Load & UIDragDropItem script?  (Read 6477 times)

ddub

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
How to use Resources.Load & UIDragDropItem script?
« on: May 29, 2014, 03:58:19 AM »
I currently instantiate various prefabs from a UI Scroll View via the handy, basically automated drag/drop system with NGUI.

I'm trying to instead have the objects loaded as a resource as they're dragged/dropped into the scene, then I'll call a Resources.UnloadUnusedAssets when they're destroyed. The UIDragDropItem script is a bit daunting and I'm not sure where to start, to get them loaded properly at the right time.

Any pointers? I'd appreciate any help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to use Resources.Load & UIDragDropItem script?
« Reply #1 on: May 29, 2014, 07:24:53 AM »
Better to start with ExampleDragDropItem. It's the script used in the NGUI's example.

ddub

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: How to use Resources.Load & UIDragDropItem script?
« Reply #2 on: May 30, 2014, 10:37:30 PM »
I do start with that actually, just don't understand where to instead insert Resources.Load and overwrite where it calls out the gameObject. Is it here?

  1. GameObject child = NGUITools.AddChild(dds.gameObject, prefab);

I guess I'm just confused with the dds. part before the gameObject call (bit nub there..), and how to properly call the load in the existing script. It is still a prefab.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to use Resources.Load & UIDragDropItem script?
« Reply #3 on: May 31, 2014, 12:49:42 PM »
ExampleDragDropItem creates the 3D object in its OnDragDropRelease function. The only difference between yours and this script would be that in your case you'd do Resources.Load instead of already having the game object prefab being referenced on the script.

ExampleDragDropSurface is just what identifies some collider as being a surface for this script. It has no code inside.

ddub

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: How to use Resources.Load & UIDragDropItem script?
« Reply #4 on: June 01, 2014, 12:32:42 AM »
Thanks for the reply -

I'm now using the public string to id the path to the prefab in the Resources folder, but it's still not working. I'm using this modified script of ExampleDragDrop, and have the prefab in my Resources folder directly:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu("NGUI/Examples/Drag and Drop Item (Example)")]
  5. public class ExampleDragDropItem : UIDragDropItem
  6. {
  7.  
  8.         public string path;
  9.  
  10.         protected override void OnDragDropRelease (GameObject surface)
  11.         {
  12.                 if (surface != null)
  13.                 {
  14.                         ExampleDragDropSurface dds = surface.GetComponent<ExampleDragDropSurface>();
  15.  
  16.                         if (dds != null)
  17.                         {
  18.                                 GameObject child = NGUITools.AddChild(dds.gameObject, Resources.Load(path));
  19.  
  20.                                 Transform trans = child.transform;
  21.                                 trans.position = UICamera.lastHit.point;
  22.  
  23.                                 if (dds.rotatePlacedObject)
  24.                                 {
  25.                                         trans.rotation = Quaternion.LookRotation(UICamera.lastHit.normal) * Quaternion.Euler(90f, 0f, 0f);
  26.                                 }
  27.                         }
  28.                 }
  29.                 base.OnDragDropRelease(surface);
  30.         }
  31. }
  32.  

Do you spot any issues there? I have 'Empty New Gameobject"'s spawning as children of my root, but not the correct prefab. It's active and everything.. just assuming I'm missing something with how the Resources.Load is integrated in the script?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to use Resources.Load & UIDragDropItem script?
« Reply #5 on: June 01, 2014, 05:29:51 PM »
Did you check to see if Resources.Load(path) actually loads your object?