Resource Management Classes

Source: Internet
Author: User

Using System;
Using Unityengine;
Using System.Collections;
Using System.Collections.Generic;

<summary>
Resource Management Classes
</summary>
public class Resmgr:monobehaviour
{

Instance references for Resmgr
private static Resmgr minstance;
Number of CPUs
private static int mprocesscount = 0;

The resource queue being loaded
Private list<resloadrequest> mloadlist = new list<resloadrequest> ();
Queue of resources waiting to be loaded
Private queue<resloadrequest> mwaitloads = new queue<resloadrequest> ();
Load the completed resource store dictionary for later cleanup
Private dictionary<string, resloadrequest> massetpackdic = new dictionary<string, resloadrequest> ();

public static Resmgr Instance
{
Get
{
return minstance;
}
}

Game Object Cache
Private Gameobject Mcachedgameobject;

Public Gameobject Cachedgameobject
{
Get
{
if (!mcachedgameobject)
{
Mcachedgameobject = This.gameobject;
}
return mcachedgameobject;
}
}

void Awake ()
{
Minstance = this;

Mprocesscount = Systeminfo.processorcount;
Mprocesscount = Mprocesscount < 1? 1:mprocesscount;
Mprocesscount = mprocesscount > 8? 8:mprocesscount;
Dontdestroyonload (Cachedgameobject);
}

void Update ()
{

Wait for the team to list the team one and load it, and then put it in the loading queue
if (Mloadlist.count > 0)
{
for (int i = mloadlist.count-1; I >= 0; i--)
{
if (Mloadlist[i].isdone)
{
Loadfinish (Mloadlist[i]);
Mloadlist.removeat (i);
}
}
}

The wait queue is placed in the load queue as soon as the request is in and the CPU has an idle thread to load
while (Mloadlist.count < mprocesscount && mwaitloads.count > 0)
{
Resloadrequest re = Mwaitloads.dequeue ();
Mloadlist.add (re);
Re. Load ();
}
}

External load resources directly through this interface, the parameters passed to the Assetpack resource load Package
public void Loadassetasync (string prefabname, Iloadlistent callBack, type type = NULL, bool Iskeepinmemory = False)
{
_loadassetasync (Prefabname, CallBack, type, iskeepinmemory);
}

private void _loadassetasync (String prefabname, Iloadlistent callBack, type type, bool iskeepinmemory)
{
if (string. IsNullOrEmpty (Prefabname))
{
if (callBack! = null) callback.failure ();
Return
}

if (Massetpackdic.containskey (prefabname))
{
if (Massetpackdic[prefabname].asset = = null)
{
if (null! = CallBack)
{
Callback.failure ();
}
}
Else
{
Callback.succeed (Massetpackdic[prefabname].asset);
}
Return
}

The traversal query is in the queue that is being loaded, if
for (int i = 0; i < Mloadlist.count; i++)
{
Resloadrequest re = mloadlist[i];
if (Re.assetName.Equals (prefabname))
{
Re. Addlistent (CallBack);
Return
}
}

Waiting to load the queue
foreach (Resloadrequest item in mwaitloads)
{
if (Item.assetName.Equals (prefabname))
{
Item. Addlistent (CallBack);
Return
}
}

Resloadrequest loadrequest = new Resloadrequest (Prefabname, iskeepinmemory, type);
LOADREQUEST.LISTENTS.ADD (CallBack);
Mwaitloads.enqueue (loadrequest);
}

The resource has finished loading the class, the processing after loading is completed
void Loadfinish (Resloadrequest Re)
{
if (Re! = null)
{
for (int i = 0; i < Re.listents.Count; ++i)
{
Iloadlistent listen = Re.listents[i];
if (listen! = NULL)
{
if (re.request! = NULL && Re.request.asset! = null)
{
Massetpackdic.add (Re.assetname, Re);
Listen. Succeed (Re.request.asset);
}
Else
{
Listen. Failure ();
}
}
}
}
}

Monitoring after resource loading is complete
public interface Iloadlistent
{
void succeed (Unityengine.object asset);

void Failure ();
}

Resource load class, which is the encapsulation of resources
public class Resloadrequest
{
Resource Name
public string Assetname;
Load callback
Public list<iloadlistent> listents = new list<iloadlistent> ();
The resources that are loaded
public resourcerequest request;
Resource type
public type type;
Whether to save in memory
public bool Iskeepinmemorry;
Add a callback to the callback list
public void Addlistent (Iloadlistent listen)
{
if (!listents. Contains (Listen))
{
Listents. ADD (listen);
}
}
The resources that are loaded
Public Unityengine.object Asset
{
Get
{
if (Request! = NULL && Request.asset! = null)
{
return request.asset;
}
Else
{
return null;
}
}
}

is currently loaded complete
public bool IsDone
{
Get
{
if (Request! = NULL)
{
return true;
}
return false;
}
}

constructor function
Public Resloadrequest (String _assetname, bool _iskeepin, Type _type)
{
This.assetname = _assetname;
This.iskeepinmemorry = _iskeepin;
This.type = _type;
}

Open load
public void Load ()
{
if (type = = NULL)
{
Type = typeof (Gameobject);
}
Request = Resources.loadasync (assetname, type);
}
}


Clean up Useless resources
public void Removeasset (string assetname, bool canremove)
{
if (!massetpackdic.containskey (assetname))
{
Return
}

if (Massetpackdic[assetname].iskeepinmemorry)
{
if (Canremove)
{
Massetpackdic[assetname] = null;
Massetpackdic.remove (Assetname);
}
}
Else
{
Massetpackdic[assetname] = null;
Massetpackdic.remove (Assetname);
}
Resources.unloadunusedassets ();
}

Force clear of all resources
public void RemoveAll ()
{
foreach (keyvaluepair<string, resloadrequest> pair in Massetpackdic)
{
Massetpackdic[pair. Key] = null;
}
Massetpackdic.clear ();
Resources.unloadunusedassets ();
}
}

Resource Management Classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.