Turn from: http://www.360doc.com/content/15/0810/11/26127157_490706154.shtml
Do uigrid dynamic loading is to do the game equipment list used, equipment information is sent from the background, the specific loading code as follows:
for (int i = 0; i < ItemData.Item_List.Count i++) {Data Datamgr = (data) itemdata.item_list;
Setgriddataiconimage (datamgr); private void Setgriddataiconimage (Data Obj) {Grid = Gameobject.find ("Uigrid").
Getcomponent<uigrid> ();
Atlas = Resources.load ("Equip/equip_atlas", typeof (Uiatlas)) as Uiatlas;
Parent = Gameobject.find ("Uigrid");
Gameobject o = resources.load ("Equipitem_prefab") as Gameobject;
Item item = o.getcomponent<item> ();
if (null = = Equipstoreobj) {Item.isremoveitem = true;
} else{item.s_item_id = Equipstoreobj.store_itemid;
Item.s_info1 = Equipstoreobj.store_info1;
Item.s_info2 = Equipstoreobj.store_info2; M_mainmodel.getatlasnameandspriteimagenamefromequipid (Equipstoreobj.info1,ref item.atlasName,ref
Item.spriteimagename); } item.
Equip.makepixelperfect ();
Nguitools.addchild (parent, O);
The list is added to refresh listview Grid.repositionnow = true; Grid.maxperliNE = 5; Re-set Uigrid grid.
Reposition (); }
The biggest problem is that when I need to update the Uigrid I need to destroy the old, add the new List content, now the correct wording is as follows:
public void Sortlv ()
{
//destroys an existing element while
(Grid.transform.childCount > 0)
{
destroyimmediate ( Grid.transform.GetChild (0). gameobject);
Add new element for
(int i = 0; i < ItemData.Item_Equip_Store_List.Count i++)
{
Equipdatamanager equiptable = ite Mdata.item_equip_store_list;
Setgriddataiconimage (equiptable);
}
At first I used the For loop to complete the deletion
for (int i = 0; i < Grid.transform.childCount i++)
{
Destroy (grid.transform.GetChild (0). gameobject);
Here are two problems, the first time to destroy the grid element of the List will automatically blank reduction, so the value of the grid.transform.childCount is always changing, the destruction of elements will be problematic, and the correct way is because Because every time the destruction is automatically reduced so in fact while the value has been decreasing, so it seems to be a dead loop, in fact, this is the correct way to write.
The difference between Destroy and destroyimmediate
The API manual says so
Destroyimmediate
Destroys the object obj immediately. It is strongly recommended to use Destroy instead.
Immediately destroy object obj, it is strongly recommended to use destroy instead.
This function should is used when writing editor code since the delayed destruction'll never be invoked in edit mod E. In game code it are recommended to use Object.destroy instead. Destroy is always delayed (but executed within the same frame) Use this function with care since it can Destroy assets manently!
This function is only used when writing editor code, because the destruction of the delay will never be invoked in edit mode. The game code is recommended using Object.destroy instead. The destruction is always deferred (but executed within the same frame), and the function is carefully used because it can permanently destroy the resource.
Destory
The object obj would be destroyed now or if it is specified t seconds. If obj is a Component it would remove the Component from the Gameobject and destroy it. If obj is a gameobject it would destroy the Gameobject, all its components and all transform children of the gameobject. Actual object destruction is always delayed until over the current Update loop, but'll always be done before rendering.
Object obj is now destroyed or destroyed after the specified T time. If obj is a component, it will destroy the component component from the Gameobject. If obj is Gameobject, it will destroy all gameobject its components and gameobject all transform child objects. The destruction of the actual object is always deferred to the current update loop, but is always completed before rendering.
Although the API manual says it is strongly recommended to use destroy instead. But when we use the Destory program decisively card dead, probably because "the destruction of the actual object is always delayed to the current update cycle", so caused the problem, so here for the time being try to use destroyimmediate.
Sort:
There are several ways to sort the uigrid itself, but I did not use, because it is to be based on the properties of equipment such as sorting, in fact, is in the first step of the loading there, there is a reading list table, and then the list of contents to add one by one to the Uigrid, then the list table reading order, the is the order of loading.
Uigrid set sort of official interface has a Custom way, seemingly to overload methods to rewrite the sorting method, seems a little trouble ~
As long as we are in the list in advance according to the order we want to do the sorting (such as bubble, fast, merge what the algorithm on their own according to the needs of the decision), and then directly uigrid loading is the order we want to
Some of the above are not clear explanation, because I did not understand the very detailed, to fully understand will continue to update the ~ should be able to help the need to use this knowledge point developers.
I would appreciate it if the great God would enlighten me ...
A beginner, just want to put the solution of their problems to write, their own to leave a backup, can also help other beginners.
Perhaps the problem is too simple, I hope the great God do not spray, there are mistakes and please point out, thank you very much.
How to eliminate the sorting caused by the cotton:
Note: When sorting due to more items, after the destruction in the List will be sorted good will be very cotton, user experience is very poor; the replacement element is also a waste of time and code, so here we find a more convenient solution.
The default sort of Uigrid is to sort by the item name in Uigrid, so if I want to sort by item level, we just need to get the item control and then modify the item's control name to a level name and then call Refresh Uigrid and he'll Automatically sorted by name order, has been tested, no cotton phenomenon.
The code is as follows:
public void Sortweight ()
{for
(int i = 0; i < ItemData.Item_List.Count; i++)
{
Itemdatamanager Itemta ble = itemdata.item_list[i];
Grid.transform.GetChild (i). Getcomponentinchildren<item> (). Name = Itemindex.getitem_weight (Grid.transform.GetChild (i). Getcomponentinchildren<item> (). item_id). ToString ();
}
The list is added to refresh listview
Grid.repositionnow = true;
Grid.maxperline = 5;
Reset Uigrid
grid.sorted = true;
Grid. Reposition ();
}
Load Prefab:
Add
gameobject theprefabobject = resources.load ("Prefabs/packitem") as Gameobject;
Add
gameobject go = Instantiate (Theprefabobject) as Gameobject;
Go.transform.parent = Tabobject.transform;
Initialization settings
Go.transform.localScale = new Vector3 (1, 1, 1);