Unity Update Resource Code

Source: Internet
Author: User

Tag: CTI private for length generic list count hand finish

Using Unityengine;
Using System.Collections;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;

public class Resupdate:monobehaviour
{
public string version_file;//= "Version.txt"; Filename
public string local_res_url;//= "file://" + Application.datapath + "/res/"; Path to local file
public string server_res_url;//= "file:///C:/Res/"; Path to the server file
public string local_res_path;//= Application.datapath + "/res/"; Local path to the game file

Private dictionary<string,string> localresversion;

Private dictionary<string, string> serverresversion;
Private list<string> needdownfiles; List of files that need to be updated
private bool Needupdatelocalversionfile = false; Whether you need to update

private void Awake ()
{
Needdownfiles = new list<string> ();
Version_file = "Version.txt";
Local_res_url = "file://" + Application.datapath + "/res/";
Server_res_url = @ "file://C:/Users/Administrator/Desktop/";
Local_res_path = Application.datapath + "/res/";
}

void Start ()
{
Initialization
Localresversion = new dictionary<string, string> ();
Serverresversion = new dictionary<string, string> ();
Needdownfiles = new List ();

Loading the local version configuration
Startcoroutine (DownLoad (Local_res_url + version_file, delegate (WWW localversion)
{
Save the local version
Parseversionfile (Localversion.text, localresversion);
Load service-side version configuration
Startcoroutine (this. DownLoad (Server_res_url + version_file, delegate (WWW ServerVersion)
{
Save service-side version
Parseversionfile (Serverversion.text, serverresversion);
Calculate the resources that need to be reloaded
Compareversion ();
Load resources that need to be updated
Downloadres ();
}));

}));
}

Load the resources that need to be updated sequentially
private void Downloadres ()
{
if (Needdownfiles.count = = 0)
{
Updatelocalversionfile ();
Return
}

string file = Needdownfiles[0];
Needdownfiles.removeat (0);

Startcoroutine (this. DownLoad (server_res_url + file, delegate (WWW W)
{
Replace the downloaded resource with a local resource
Replacelocalres (file, w.bytes);
Downloadres ();
}));
}

private void Replacelocalres (String fileName, byte[] data)
{
String FilePath = Local_res_path + fileName;
FileStream stream = new FileStream (Local_res_path + fileName, filemode.create);
Stream. Write (data, 0, data. Length);
Stream. Flush ();
Stream. Close ();
}

Show Resources
Private IEnumerator Show ()
{
yield return null;
www asset = new www (local_res_url + "Cube.assetbundle");
yield return asset;
Assetbundle bundle = Asset.assetbundle;
Instantiate (bundle. Loadasset ("Cube"));
Bundle. Unload (FALSE);
}

Update the local version configuration
private void Updatelocalversionfile ()
{
if (needupdatelocalversionfile)
{
StringBuilder versions = new StringBuilder ();
foreach (var item in serverresversion)
{
Versions. Append (item. Key). Append (","). Append (item. Value). Append ("\ n");
}

FileStream stream = new FileStream (Local_res_path + version_file, filemode.create);
byte[] data = Encoding.UTF8.GetBytes (versions. ToString ());
Stream. Write (data, 0, data. Length);
Stream. Flush ();
Stream. Close ();
}
Loading Display objects
Startcoroutine (Show ());
}

private void Compareversion ()
{
foreach (var version in serverresversion)
{
String fileName = version. Key;
String serverMd5 = version. Value;
New resources
if (! Localresversion.containskey (FileName))
{
Needdownfiles.add (FileName);
}
Else
{
Resources that need to be replaced
String LocalMd5;
Localresversion.trygetvalue (FileName, out localMd5); Determine the name of the file in the server from the local file and return to the MD5
if (!servermd5.equals (LOCALMD5))//If this MD5 in the server is not equal to the local
{
Needdownfiles.add (FileName); Add to Collection
}
}
}
This time update, and update the local version.txt
Needupdatelocalversionfile = needdownfiles.count > 0; Determine if the resource to be updated and added is greater than 0 assigned to the BOOL judgment variable
}

private void Parseversionfile (string content, dictionary<string, string> dict)
{
if (content = = NULL | | content. Length = = 0)
{
Return
}
string[] items = content. Split (new char[] {' \ n '});
foreach (string item in items)
{
string[] Info = Item. Split (new char[] {', '});
if (info! = NULL && info. Length = = 2)
{
Dict. ADD (Info[0], info[1]);
}
}
}

Private IEnumerator DownLoad (string url, handlefinishdownload finishfun)
{
www www = new www (URL);
yield return www;
if (finishfun! = null)
{
Finishfun (WWW);
}
Www.Dispose ();
}

public delegate void Handlefinishdownload (www www);
}

Unity Update Resource Code

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.