Difference between revisions of "Custom schema"

From PowerUI
Jump to: navigation, search
(Created page with "Custom protocols:// (officially called a 'schema') are a great way of extending PowerUI. By default it comes with a few non-standard ones: * Resources Protocol|resources://...")
 
(Creating your own)
Line 43: Line 43:
 
         }
 
         }
 
 
/// <summary>Get the raw data.</summary>
+
    /// <summary>Get the raw data.</summary>
public override void OnGetData(ContentPackage package){
+
    public override void OnGetData(ContentPackage package){
 
 
// Path without the protocol:
+
    // Path without the protocol:
string rawPath=package.location.Path;
+
    string rawPath=package.location.Path;
 
 
// If the path is ok..
+
    // If the path is ok..
if(rawPath=="test"){
+
    if(rawPath=="test"){
  
// Some random data:
+
    // Some random data:
byte[] data=new byte[4];
+
    byte[] data=new byte[4];
 
 
// Optionally add some extra headers:
+
    // Optionally add some extra headers:
package.responseHeaders["Hello"]="My extra header";
+
    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:
+
    // Optionally state that the headers have been received (if this happens ahead of time) along with the content length:
package.ReceivedHeaders(data.Length);
+
    package.ReceivedHeaders(data.Length);
 
 
// The important line! Say we've received the data:
+
    // The important line! Say we've received the data:
package.ReceivedData(data,0,data.Length);
+
    package.ReceivedData(data,0,data.Length);
 
 
}else{
+
    }else{
 
 
// If we couldn't find it..
+
    // If we couldn't find it..
package.Failed(404);
+
    package.Failed(404);
 
 
}
+
    }
  
// - Advanced - Handling partial (range) responses
+
    // - Advanced - Handling partial (range) responses
/*
+
    /*
int partialStart;
+
    int partialStart;
int partialEnd;
+
    int partialEnd;
package.GetRange(out partialStart,out partialEnd)
+
    package.GetRange(out partialStart,out partialEnd)
See DriveProtocol.cs (file://) for more info
+
    See DriveProtocol.cs (file://) for more info
*/
+
    */
  
}
+
    }
 
        
 
        
 
     }
 
     }

Revision as of 20:35, 13 January 2017

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!)

Returns an image; specifically the output of the named camera

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
		    */

	    }
       
    }
   
}