Custom schema

From PowerUI
Jump to: navigation, search

Custom protocols:// (officially called a 'schema') are a great way of extending PowerUI. By default it comes with a few non-standard ones:

Loads the named content from any Resources folder within your project (you can have multiple folders called Resources). The most heavily used protocol in PowerUI.

Loads the named scene (it must be included in build settings!)

Opens up a widget. These are the same.

Used to display a resized image

Returns an image; specifically the output of the named camera

The ImageCache which lets you display any generic Texture2D or RenderTexture on your UI. Largely depreciated - nowadays just use anElement.image = yourTexture; to set the background image to a Unity texture.

Accesses dynamic textures which are being written to by code

Accesses content within an AssetBundle.


Creating your own

For inspiration, you'll find the built in ones here:

PowerUI\Source\File Protocols

All you need to do is add a C# class which inherits FileProtocol. PowerUI will automatically find it for you. Here's an example; it's a simplified version of file://:

using UnityEngine;
using Css;
using Dom;


namespace PowerUI{
    
    public class MyProtocol:FileProtocol{
       
        /// <summary>Returns all protocol names:// that can be used for this protocol.</summary>
        public override string[] GetNames(){
            return new string[]{"myprotocol"};
        }
		
	    /// <summary>Get the raw data.</summary>
	    public override void OnGetData(ContentPackage package){
			
		    // Path without the protocol:
		    string rawPath=package.location.Path;
		
 		    // If the path is ok..
		    if(rawPath=="test"){

			    // Some random data:
			    byte[] data=new byte[4];
			
			    // Optionally add some extra headers:
			    package.responseHeaders["Hello"]="My extra header";
			
			    // Optionally state that the headers have been received (if this happens ahead of time) along with the content length:
			    package.ReceivedHeaders(data.Length);
			
			    // The important line! Say we've received the data:
			    package.ReceivedData(data,0,data.Length);
		
		    }else{
		
			    // If we couldn't find it..
			    package.Failed(404);
		
		    }

		    // - Advanced - Handling partial (range) responses
		    /*
		    int partialStart;
		    int partialEnd;
		    package.GetRange(out partialStart,out partialEnd)
		    See DriveProtocol.cs (file://) for more info
		    */

	    }
       
    }
   
}