No read access to the texture data - WWW In WEBPLAYER!

I am facing this problem a long time!
Using the WWW class I download an image of my host.
When I try to manipulate this image through its pixels, I get this error: SecurityException: No read access to the texture data:!

I understand that this error telling me that I’m not allowed to read and or change the pixels of this image.
http://www.royalpoker.com.br/crossdomain.xml

My TestOnline is accessed on the link:
http://www.royalpoker.com.br/game/webfoto
In the Editor, using the WWW Security Emulation:

If I test on the Browser, or if I test without Security emulation of the editor, I get the error!

Remembering all of you, I have the file release “Crossdomain”, on the host, as I mentioned in the link above!

crossdomain.xml

<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>

Please test yourself the link I posted. May see the example with a console that returning the same messages of DEBUG Editor!

I’m trying to fix this problem about 6 months and so far, no one has been able to answer me something functional!

MY COMPLETE SCRIPT FOR TEST:
Mask Image for use on sample:
http://www.royalpoker.com.br/game/webfoto/avatarMask.png

using UnityEngine;
using System.Collections;
using System;
using System.Threading;


public class webAvatar : MonoBehaviour
{
    public Texture2D tmp { get; set; }

    string url = "http://www.royalpoker.com.br/julex.jpg";
    public Texture2D mask; //MaskImage for cutOut

    void Start()
    {
        StartCoroutine(LoadAvatar());
    }

    void OnGUI()
    {
        if (tmp != null)
            GUI.DrawTexture(new Rect(10, 10, 128, 128), tmp);
    }

    //Load Avatar from WWW
    IEnumerator LoadAvatar()
    {
        WWW www = new WWW(url);

        yield return www;

        if (www.error != null)
        {
            Debug.LogWarning(www.error);
        }
        else
        {
            Texture2D newT = www.texture;

            //www.LoadImageIntoTexture(tmp);
            tmp = CreateUsingMaskAlpha(newT, mask);
        }
    }

    //Create Avatar Icon from MASK
    Texture2D CreateUsingMaskAlpha(Texture2D diffuse, Texture2D mask)
    {

        TextureScale.Bilinear(diffuse, 128, 128);

        Texture2D result = new Texture2D(diffuse.width, diffuse.height, TextureFormat.ARGB32, false);
            

        Color[] diffuseColors = diffuse.GetPixels();
        Color[] maskColors = mask.GetPixels();

        for (int i = 0; i < diffuseColors.Length; i++)
        {
            if (i < maskColors.Length)
            {
                diffuseColors[i].a = maskColors[i].a;
            }
        }

        result.SetPixels(diffuseColors);
        result.Apply();

        Debug.Log("Texture"+result.name);
        Debug.Log(result.width + "x" + result.height);

        return result;
    }
 
    //Function for Read and Write pixel for the modfy image origem to avatar
    public class TextureScale
    {
        public class ThreadData
        {
            public int start;
            public int end;
            public ThreadData(int s, int e)
            {
                start = s;
                end = e;
            }
        }

        private static Color[] texColors;
        private static Color[] newColors;
        private static int w;
        private static float ratioX;
        private static float ratioY;
        private static int w2;
        private static int finishCount;
        private static Mutex mutex;

        public static void Point(Texture2D tex, int newWidth, int newHeight)
        {
            ThreadedScale(tex, newWidth, newHeight, false);
        }

        public static void Bilinear(Texture2D tex, int newWidth, int newHeight)
        {
            ThreadedScale(tex, newWidth, newHeight, true);
        }

        private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
        {
            texColors = tex.GetPixels();
            newColors = new Color[newWidth * newHeight];
            if (useBilinear)
            {
                ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
                ratioY = 1.0f / ((float)newHeight / (tex.height - 1));
            }
            else
            {
                ratioX = ((float)tex.width) / newWidth;
                ratioY = ((float)tex.height) / newHeight;
            }
            w = tex.width;
            w2 = newWidth;
            var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
            var slice = newHeight / cores;

            finishCount = 0;
            if (mutex == null)
            {
                mutex = new Mutex(false);
            }
            if (cores > 1)
            {
                int i = 0;
                ThreadData threadData;
                for (i = 0; i < cores - 1; i++)
                {
                    threadData = new ThreadData(slice * i, slice * (i + 1));
                    ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
                    Thread thread = new Thread(ts);
                    thread.Start(threadData);
                }
                threadData = new ThreadData(slice * i, newHeight);
                if (useBilinear)
                {
                    BilinearScale(threadData);
                }
                else
                {
                    PointScale(threadData);
                }
                while (finishCount < cores)
                {
                    Thread.Sleep(1);
                }
            }
            else
            {
                ThreadData threadData = new ThreadData(0, newHeight);
                if (useBilinear)
                {
                    BilinearScale(threadData);
                }
                else
                {
                    PointScale(threadData);
                }
            }

            tex.Resize(newWidth, newHeight);
            tex.SetPixels(newColors);
            tex.Apply();
        }

        public static void BilinearScale(System.Object obj)
        {
            ThreadData threadData = (ThreadData)obj;
            for (var y = threadData.start; y < threadData.end; y++)
            {
                int yFloor = (int)Mathf.Floor(y * ratioY);
                var y1 = yFloor * w;
                var y2 = (yFloor + 1) * w;
                var yw = y * w2;

                for (var x = 0; x < w2; x++)
                {
                    int xFloor = (int)Mathf.Floor(x * ratioX);
                    var xLerp = x * ratioX - xFloor;
                    newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
                                                           ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
                                                           y * ratioY - yFloor);
                }
            }

            mutex.WaitOne();
            finishCount++;
            mutex.ReleaseMutex();
        }

        public static void PointScale(System.Object obj)
        {
            ThreadData threadData = (ThreadData)obj;
            for (var y = threadData.start; y < threadData.end; y++)
            {
                var thisY = (int)(ratioY * y) * w;
                var yw = y * w2;
                for (var x = 0; x < w2; x++)
                {
                    newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
                }
            }

            mutex.WaitOne();
            finishCount++;
            mutex.ReleaseMutex();
        }

        private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
        {
            return new Color(c1.r + (c2.r - c1.r) * value,
                              c1.g + (c2.g - c1.g) * value,
                              c1.b + (c2.b - c1.b) * value,
                              c1.a + (c2.a - c1.a) * value);
        }
    }
}

For the love of Jesus Christ. Someone be so kind to test my example and give me a solution for this problem? I have this problem a long time and he’s totally affecting my project!
I did everything I found on google and here in the community and absolutely nothing solves !

Tankx brother!.
I Wating!

See: http://docs.unity3d.com/Manual/SecuritySandbox.html

There is a Debugging section. Please follow that and post the extra data that gets recorded.

Please friend!
Have you tested the example I posted?
can tell me if it works with you? Because I believe it’s something with my Unity, or maybe of the engine itself

I had this procedure done to see the LOGS referring to crossdomain file, but the return of my file does not look like anything, with what is described in the documentation of UNITY!
See It:

And LOG:
C:\Users.…\AppData\Local\Unity\Editor

UnityLOG

----- Total AssetImport time: 0.034742s, Asset Import: 0.000000s, CacheServerIntegrate: 0.000000s, CacheServer Download: 0.000000s [0 B, -1.#IND00 mb/s], CacheServer Hashing: 0.000000s [0 B, -1.#IND00 mb/s]

Requesting http://update.unity3d.com/4.3/ivy.xml

Platform assembly: C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\System.Xml.Linq.dll (this message is harmless)
Downloading into C:\Users\JUS2DE~1\AppData\Local\Temp\unity\69dee4b8-41ff-4390-92f5-b5394893d4ab\ivy.xml

Mono: successfully reloaded assembly

Platform assembly: C:\Program Files (x86)\Unity\Editor\Data\Managed\CrossDomainPolicyParser.dll (this message is harmless)
Throwing this exception: System.Security.SecurityException: Unexpected error while trying to call method_GetSecurityPolicyBlocking : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> MonoForks.System.UriFormatException: Absolute URI is too short

at MonoForks.System.Uri.Parse (UriKind kind, System.String uriString) [0x00000] in :0

at MonoForks.System.Uri.ParseUri (UriKind kind) [0x00000] in :0

at MonoForks.System.Uri.Parse () [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString, Boolean dontEscape) [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString) [0x00000] in :0

at (wrapper remoting-invoke-with-check) MonoForks.System.Uri:.ctor (string)

at MonoForks.System.Windows.Interop.PluginHost.get_SourceUri () [0x00000] in :0

at MonoForks.System.Windows.Browser.Net.CrossDomainPolicyManager.GetCachedWebPolicy (MonoForks.System.Uri uri) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicy (System.String requesturi_string, IPolicyProvider policyProvider) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicyForDotNetWebRequest (System.String requesturi_string, System.Reflection.MethodInfo policyProvidingMethod) [0x00000] in :0

at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[ ],System.Exception&)

at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) [0x000d0] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222

— End of inner exception stack trace —

at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232

at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters) [0x00000] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115

at System.Net.WebConnection.CheckUnityWebSecurity (System.Net.HttpWebRequest request) [0x00000] in :0 System memory in use before: 25.5 MB.

CheckingSecurityForUrl: http://update.unity3d.com/4.3/ivy.xml

Throwing this exception: System.Security.SecurityException: Unexpected error while trying to call method_GetSecurityPolicyBlocking : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> MonoForks.System.UriFormatException: Absolute URI is too short

at MonoForks.System.Uri.Parse (UriKind kind, System.String uriString) [0x00000] in :0

at MonoForks.System.Uri.ParseUri (UriKind kind) [0x00000] in :0

at MonoForks.System.Uri.Parse () [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString, Boolean dontEscape) [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString) [0x00000] in :0

at (wrapper remoting-invoke-with-check) MonoForks.System.Uri:.ctor (string)

at MonoForks.System.Windows.Interop.PluginHost.get_SourceUri () [0x00000] in :0

at MonoForks.System.Windows.Browser.Net.CrossDomainPolicyManager.GetCachedWebPolicy (MonoForks.System.Uri uri) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicy (System.String requesturi_string, IPolicyProvider policyProvider) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicyForDotNetWebRequest (System.String requesturi_string, System.Reflection.MethodInfo policyProvidingMethod) [0x00000] in :0

at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[ ],System.Exception&)

at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) [0x000d0] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222

— End of inner exception stack trace —

at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232

at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters) [0x00000] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115

at System.Net.WebConnection.CheckUnityWebSecurity (System.Net.HttpWebRequest request) [0x00000] in :0 Unloading 3 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)

System memory in use after: 23.4 MB.

Unloading 3 unused Assets to reduce memory usage. Loaded Objects now: 1675.
Total: 39.922966 ms (FindLiveObjects: 0.175873 ms CreateObjectMapping: 0.014976 ms MarkObjects: 3.777830 ms DeleteObjects: 0.039552 ms)

UriFormatException: Absolute URI is too short
at MonoForks.System.Uri.Parse (UriKind kind, System.String uriString) [0x00000] in :0

at MonoForks.System.Uri.ParseUri (UriKind kind) [0x00000] in :0

at MonoForks.System.Uri.Parse () [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString, Boolean dontEscape) [0x00000] in :0

at MonoForks.System.Uri…ctor (System.String uriString) [0x00000] in :0

at (wrapper remoting-invoke-with-check) MonoForks.System.Uri:.ctor (string)

at MonoForks.System.Windows.Interop.PluginHost.get_SourceUri () [0x00000] in :0

at MonoForks.System.Windows.Browser.Net.CrossDomainPolicyManager.GetCachedWebPolicy (MonoForks.System.Uri uri) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicy (System.String requesturi_string, IPolicyProvider policyProvider) [0x00000] in :0

at UnityEngine.UnityCrossDomainHelper.GetSecurityPolicy (System.String requesturi_string) [0x00000] in :0
(Filename: Line: -1)

SecurityException: No read access to the texture data:
at (wrapper managed-to-native) UnityEngine.Texture2D:GetPixels (int,int,int,int,int)

at UnityEngine.Texture2D.GetPixels (Int32 miplevel) [0x0002a] in C:\BuildAgent\work\d3d49558e4d408f4\artifacts\EditorGenerated\TextureBindings.cs:234

at UnityEngine.Texture2D.GetPixels () [0x00002] in C:\BuildAgent\work\d3d49558e4d408f4\artifacts\EditorGenerated\TextureBindings.cs:227

at webAvatar+TextureScale.ThreadedScale (UnityEngine.Texture2D tex, Int32 newWidth, Int32 newHeight, Boolean useBilinear) [0x00000] in D:\UPROJECT\avatar_test\Assets\webAvatar.cs:110

at webAvatar+TextureScale.Bilinear (UnityEngine.Texture2D tex, Int32 newWidth, Int32 newHeight) [0x00000] in D:\UPROJECT\avatar_test\Assets\webAvatar.cs:105

at webAvatar.CreateUsingMaskAlpha (UnityEngine.Texture2D diffuse, UnityEngine.Texture2D mask) [0x00000] in D:\UPROJECT\avatar_test\Assets\webAvatar.cs:50

at webAvatar+c__Iterator0.MoveNext () [0x00085] in D:\UPROJECT\avatar_test\Assets\webAvatar.cs:42
(Filename: Assets/webAvatar.cs Line: 110)

Nobody?
Please friends. At least test the example I posted, and let me know if it works for you, or does not work.
This can be some of the ENGUINE Bug!

http://www.royalpoker.com.br/julex.jpg on line 12 gives 404 error not found. Got another file to test with?

Im Sory! I Change location.

I was testing weekend and ended up changing the location of the photo
However, I put it in the same place!

I also have the same picture on:
http://www.royalpoker.com.br/game/webfoto/julex.jpg
I also have the same crossdomain on:
http://www.royalpoker.com.br/game/webfoto/crossdomain.xml

I already did everything that exists in the documentation and also already tested everything I could find on google, but nothing works for min!
I needed to know if this is global and this not working for anyone or is it only here!

I updated my Unity but the same problem occurs.

@neoneper Your http://www.royalpoker.com.br/crossdomain.xml file is the problem. I tested using my own server and had the same failure as you with that crossdomain.xml file. This is how to make it work. Change your crossdomain.xml in the root of www.royalpoker.com.br to:

<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>

Notice I removed to-ports=“1200-1220”. Now it works.

The crossdomain.xml file at http://www.royalpoker.com.br/game/webfoto/crossdomain.xml is not doing anything at all and probably just confusing you. Delete it. That crossdomain.xml file has the correct content, but it is in the wrong place. Unity will only load the crossdomain.xml file that is in the root folder of the domain.

1 Like

The cross domain policy you have at the root level of your server is for sockets. It’s meant to be a www policy file.

What @guavaman said.

for me does not work!!

SEE:
http://www.royalpoker.com.br/crossdomain.xml
http://www.royalpoker.com.br/game/webfoto/

I put the xml the way you stated.
Removed the same file, of the “webfoto” folder to avoid conflicts.

But nothing changed !.
The Same error happens locally and also in HOSTING. You can check the example of accessing on LINK!

He should open the photo because it’s all ok! But the error message is the same.

I believe that the problem is on my computer.
If you managed to make this example work for you, so this problem is here. When I compile with my UNITY, a problem may be occurring.

I updated my Unity to the latest version, but nothing changed!

The problem is not on your computer. I found that while it works in the editor, it does not work in a build. Your example webplayer link doesn’t work either, but the error message is different:

Method access exception: Attempt to access a private/protected method failed

I’ll see if I can figure out why.

It’s failing on line 130 under ThreadedScale

mutex = newMutex(false);

According to this: WebPlayer + Multi-threading? - Unity Engine - Unity Discussions

1 Like

HoMyGOD!. What you said is actually true. I had not noticed this!
I was using this to modify the scale of Texture2D in CROP!
http://wiki.unity3d.com/index.php/TextureScale

NOW, At this moment I have a huge problem (^.~) LOL!
I do not know any other way to modify the scale of a 2D texture in Unity!

You let me know if there is something in this genre for Unity?
I thank you infinitely for your patience in helping me. Thank you friend!

Ohhhh… WOOORKKKK!
Lock instead of Mutex and WOOOOOOORKKKKK… TAAAAAAAAAANKX MAAAAAAAAAAAAANnn
! I LOVE YOUUU
http://www.royalpoker.com.br/game/webfoto/

You could just strip out the multithreading like this:

public class TextureScale {

    private static Color[] texColors;
    private static Color[] newColors;
    private static int w;
    private static float ratioX;
    private static float ratioY;
    private static int w2;

    public static void Point(Texture2D tex, int newWidth, int newHeight) {
        Scale(tex, newWidth, newHeight, false);
    }

    public static void Bilinear(Texture2D tex, int newWidth, int newHeight) {
        Scale(tex, newWidth, newHeight, true);
    }

    private static void Scale(Texture2D tex, int newWidth, int newHeight, bool useBilinear) {
        texColors = tex.GetPixels();
        newColors = new Color[newWidth * newHeight];

        if(useBilinear) {
            ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
            ratioY = 1.0f / ((float)newHeight / (tex.height - 1));
        } else {
            ratioX = ((float)tex.width) / newWidth;
            ratioY = ((float)tex.height) / newHeight;
        }
  
        w = tex.width;
        w2 = newWidth;

        if(useBilinear) {
            BilinearScale(newHeight);
        } else {
            PointScale(newHeight);
        }

           tex.Resize(newWidth, newHeight);
            tex.SetPixels(newColors);
           tex.Apply();
        }

    public static void BilinearScale(int newHeight) {
        for(var y = 0; y < newHeight; y++) {
            var yFloor = Mathf.FloorToInt(y * ratioY);
            var y1 = yFloor * w;
            var y2 = (yFloor + 1) * w;
            var yw = y * w2;

            for(var x = 0; x < w2; x++) {
                var xFloor = Mathf.FloorToInt(x * ratioX);
                var xLerp = x * ratioX - xFloor;
                newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
                ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
  y * ratioY - yFloor);
            }
        }
    }

    public static void PointScale(int newHeight) {
        for(var y = 0; y < newHeight; y++) {
            var thisY = (int)(ratioY * y) * w;
            var yw = y * w2;
            for(var x = 0; x < w2; x++) {
                newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
            }
        }
    }

    private static Color ColorLerpUnclamped(Color c1, Color c2, float value) {
        return new Color(c1.r + (c2.r - c1.r) * value,
        c1.g + (c2.g - c1.g) * value,
        c1.b + (c2.b - c1.b) * value,
        c1.a + (c2.a - c1.a) * value);
    }
}

But it looks like you got it working anyway. Glad to help!

1 Like

oyaaaaa mannn tankx brother. its work to! (^.~) i lOVE mAN!.. Tank Very Much!..
You released your time to help me and it is very nice of you!
Heartfelt thanks !.