Example #1
0
        /// <summary>Applies an alpha mask over the camera itself. This can be used to shape the camera
        /// into e.g. a circle.</summary>
        /// <param name="image">The mask image. Originates from the mask="file_path" attribute.</param>
        public void SetMask(Texture image)
        {
            if (htmlDocument.AotDocument)
            {
                return;
            }

            // Main thread only (as it checks if Mask is null):
            Callback.MainThread(delegate(){
                if (Mask == null)
                {
                    // Create the gameobject.

                    if (image == null)
                    {
                        return;
                    }

                    // Create the object:
                                        #if PRE_UNITY4
                    GameObject maskObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
                                        #else
                    GameObject maskObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
                                        #endif

                    // Remove the MC:
                    MeshCollider collider = maskObject.GetComponent <MeshCollider>();

                    if (collider != null)
                    {
                        // Remove it:
                        GameObject.Destroy(collider);
                    }

                    // Grab the transform:
                    Mask = maskObject.transform;

                    // Set the name:
                    maskObject.name = "#PowerUI-Mask";

                    // If possible, parent it to the camera now:
                    ParentMask();

                    // Grab the renderer:
                    MeshRenderer renderer = maskObject.GetComponent <MeshRenderer>();

                    // Grab the material:
                    Material material = renderer.material;

                    // Apply the shader:
                    material.shader = MaskShader;

                    // Set the offset - it must be the first thing rendered always:
                    material.renderQueue = 1;

                    // Apply the mask texture:
                    material.SetTexture("_Mask", image);
                }
            });
        }
        public override void OnGetGraphic(ImagePackage package)
        {
            // Main thread only:
            Callback.MainThread(delegate(){
                string resUrl = package.location.Directory + package.location.Filename;

                if (resUrl.Length > 0 && resUrl[0] == '/')
                {
                    resUrl = resUrl.Substring(1);
                }

                // Get the image:
                UnityEngine.Object resource = Resources.Load(resUrl);

                if (resource == null)
                {
                    // Note: the full file should be called something.bytes for this to work in Unity.
                    resUrl = package.location.Path;

                    if (resUrl.Length > 0 && resUrl[0] == '/')
                    {
                        resUrl = resUrl.Substring(1);
                    }

                    resource = Resources.Load(resUrl);
                }

                // Try loading from the asset:
                if (package.Contents.LoadFromAsset(resource, package))
                {
                    package.Done();
                }
            });
        }
        public override void OnGetDataNow(ContentPackage package)
        {
            // Main thread only:
            Callback.MainThread(delegate(){
                // Getting a files text content from resources.
                byte[] data = null;

                string path = GetPath(package.location);

                TextAsset asset = Resources.Load(path) as TextAsset;

                if (asset == null)
                {
                    // Not found
                    package.Failed(404);
                }
                else
                {
                    // Ok
                    data = asset.bytes;
                    package.ReceivedHeaders(data.Length);
                    package.ReceivedData(data, 0, data.Length);
                }
            });
        }
        public override void OnGetGraphic(ImagePackage package)
        {
            // Already resized?
            ResizedImage resized = ResizedImages.Get(package.location.Path);

            if (resized != null)
            {
                // Sure is!
                package.GotGraphic(resized.Image);

                return;
            }

            // Main thread only:
            Callback.MainThread(delegate(){
                // Try loading from resources:
                string resUrl = package.location.Directory + package.location.Filename;

                if (resUrl.Length > 0 && resUrl[0] == '/')
                {
                    resUrl = resUrl.Substring(1);
                }

                // Get the image:
                UnityEngine.Object resource = Resources.Load(resUrl);

                if (resource == null)
                {
                    // Note: the full file should be called something.bytes for this to work in Unity.
                    resource = Resources.Load(package.location.Path);
                }

                if (!package.Contents.LoadFromAsset(resource, package))
                {
                    return;
                }

                PictureFormat pict = package.Contents as PictureFormat;

                if (pict != null)
                {
                    // Resize the image:
                    resized = ResizedImages.Add(package.location.Path, pict.Image as Texture2D);

                    // Apply:
                    pict.Image = resized.Image;
                }

                // Great, stop there:
                package.Done();
            });
        }
Example #5
0
        /// <summary>Use this to combine all the data into one buffer.
        /// Call it from RecieveData and override ReceivedAllData to get that single result.</summary>
        public void ReceiveAllData(byte[] buffer, int offset, int count)
        {
            if (readyState_ == 1)
            {
                // No headers received; trigger it anyway:
                ReceivedHeaders();
            }

            if (_stream == null)
            {
                _stream = new System.IO.MemoryStream();
            }

            bytesReceived += count;

            _stream.Write(buffer, offset, count);

            if (readyState == 4)
            {
                return;
            }

            bool done = (contentLength == -1) ? (count == 0) : (bytesReceived >= contentLength);

            if (done)
            {
                // Received all of the data!
                byte[] data = _stream.ToArray();
                _stream = null;
                Callback.MainThread(delegate(){
                    ReceivedAllData(data);
                    readyState = 4;
                });
            }
            else
            {
                readyState = 3;
            }
        }
Example #6
0
        public override bool OnAttributeChange(string property)
        {
            if (base.OnAttributeChange(property))
            {
                return(true);
            }

            if (property == "path")
            {
                // Go get the camera now!

                // Clear any existing one:
                Camera = null;

                Callback.MainThread(delegate(){
                    // Grab the path itself:
                    string path = this["path"];

                    // Get it:
                    GameObject gameObject = GameObject.Find(path);

                    if (gameObject != null)
                    {
                        // Grab the camera:
                        Camera = gameObject.GetComponent <Camera>();
                    }

                    if (Camera != null)
                    {
                        ComputedStyle computed = Style.Computed;

                        // Create RT if one is needed:
                        RenderTexture rt = Camera.targetTexture;

                        if (rt == null)
                        {
                            // Apply:
                            ApplyNewRenderTexture((int)computed.InnerWidth, (int)computed.InnerHeight);
                        }
                        else
                        {
                            // Apply to background:
                            image = rt;
                        }
                    }

                    ParentMask();
                });

                return(true);
            }
            else if (property == "noresize")
            {
                // Can't resize if noresize is not null:
                CanResize = (this["noresize"] == null);
            }
            else if (property == "mask")
            {
                // We've got a mask!

                // Grab the file path:
                string maskFile = this["mask"];

                if (maskFile == null)
                {
                    SetMask(null);
                }
                else
                {
                    // Create a package to get the mask:
                    ImagePackage package = new ImagePackage(maskFile, document.basepath);

                    package.onload = delegate(UIEvent e){
                        // Apply the mask:
                        PictureFormat pict = package.Contents as PictureFormat;

                        if (pict != null)
                        {
                            SetMask(pict.Image);
                        }
                    };

                    // Go get it:
                    package.send();
                }

                return(true);
            }

            return(false);
        }
        /// <summary>Gets a bundle using the given bundle URI.</summary>
        private void LoadBundle(Location uri, BundleReadyEvent bre)
        {
            // Main thread only. (Even things like bund!=null can fail).
            Callback.MainThread(delegate(){
                // The underlying uri is uri.Path.
                string path = uri.Path;

                // Loading or loaded?
                AssetBundle bund = Bundles.Get(path);

                if (bund != null)
                {
                    bre(bund);
                    return;
                }

                DataPackage package;

                if (Bundles.Loading.TryGetValue(path, out package))
                {
                    // Loading - just add a listener (always runs after the bundle loading callback):
                    package.addEventListener("onload", delegate(UIEvent e){
                        // Callback:
                        bre(Bundles.Get(path));
                    });
                }
                else
                {
                    // Make a request:
                    package = new DataPackage(path, null);

                    package.addEventListener("onload", delegate(UIEvent e){
                        // 5.4.1 onwards
                                                #if !UNITY_5_4_0 && UNITY_5_4_OR_NEWER
                        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(package.responseBytes);
                                                #else
                        AssetBundleCreateRequest request = AssetBundle.CreateFromMemory(package.responseBytes);
                                                #endif

                        // Get the enumerator:
                        IEnumerator enumerator = Loader(request);

                        // Add updater:
                        OnUpdateCallback cb = null;

                        cb = OnUpdate.Add(delegate(){
                            // Move enumerator:
                            enumerator.MoveNext();

                            // Request done?
                            if (request.isDone)
                            {
                                // Great! Stop:
                                cb.Stop();

                                // Set now:
                                AssetBundle bundle = request.assetBundle;

                                Bundles.Add(path, bundle);

                                // Callback:
                                bre(bundle);
                            }
                        });
                    });

                    // Send now:
                    package.send();
                }
            });
        }