Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Political Peace Party Studios · Feb 27, 2016 at 01:24 AM · bugiphonexcodegetpixels

Xcode Problem (Whats Wrong With My Code)

So we are creating a scratch card type effect in our app. This app is already out on android and the effect is working perfect. Now we are trying to get it on iPhone, but we keep running into a error with the scratch card effect on Xcode. It works on Unity editor and on Unity Remote for IOS but it won't do the effect when on the device alone. Heres our Scratch Card Effect code. The build is always successful, the scratch card effect just does not work.

 using UnityEngine;
 using System.Collections;
 
 public enum RefreshRate
 {
     _10_FPS = 10,
     _20_FPS = 20,
     _30_FPS = 30,
     _40_FPS = 40,
     _50_FPS = 50,
     _60_FPS = 60,
 }
 
 public class ScratchIt : MonoBehaviour
 {
 public bool interpolateMovement = true;
 public int brushSize = 30;
 public RefreshRate refreshRate = RefreshRate._30_FPS;
 [SerializeField, Range(10, 500)] int maxCycleByInterpolation = 100;
 [SerializeField, Range(1, 10)] int alphaCheckPrecision = 5;
 [SerializeField] float scratchCompleted;
 public float ScratchCompleted
 {
     get {return scratchCompleted;}
 }
 Color[] brush;
 Color32[] data;
 Texture2D tex;
 float timer;
 Vector2 oldPosition;
 RenderTexture rt;
 Texture2D current;
 
 void Start ()
 {
     oldPosition = Vector2.zero;
     timer = 0;
     current = renderer.material.GetTexture(0) as Texture2D;
     ResetCovering();
     int tabSize = current.width * current.height;
     brush = new Color[tabSize];
     for (int i = 0; i < tabSize; i++)
         brush[i] = new Color(0, 0, 0, 0);
 }
 
 
 /// <summary>
 /// Resets the covering. The image goes back to it's original texture
 /// </summary>
 public void ResetCovering()
 {
     data = current.GetPixels32();
     Texture2D newTex = new Texture2D(current.width, current.height);
     newTex.SetPixels32(data);
     renderer.material.SetTexture(0, newTex);
     tex = newTex;
     tex.Apply();
     updatePercentage();
 }
   
 void Update ()
 {
 
     if (ScratchCompleted >= 0.6) {
 
         Debug.Log("YOU WON!!");
 
     }
 
     timer += Time.deltaTime;
     if (Input.GetMouseButton(0))
     {
         Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         Physics.Raycast(r, out hit);
         try
         {
             if (hit.collider.gameObject == this.gameObject)
             {
                 if (!interpolateMovement)
                     scratch(new Vector2(hit.textureCoord.x * tex.width, hit.textureCoord.y * tex.height));
                 if (timer > (1f / (float)refreshRate))
                 {
                     if (interpolateMovement)
                         scratch(new Vector2(hit.textureCoord.x * tex.width, hit.textureCoord.y * tex.height));
                     updatePercentage();
                     tex.Apply();
                     timer = 0;
                 }
             }
         }
         catch {
         }
     }
 }
 
 public void updatePercentage()
 {
     Color32[] tmp = tex.GetPixels32(); // __________  SLOW  __________
     float value = 0;
     for (int i = 0; i < tmp.Length; i += alphaCheckPrecision)
         value += tmp[i].a;
     scratchCompleted = 1 - value / (255 * data.Length / 5);
 
 }
 
 
 void scratchInterpolated(Vector2 position)
 {
 
     if (Input.GetMouseButtonDown(0))
         oldPosition = position;
     int i = 0;
     Vector2 tmpPos = oldPosition;
     Vector2 dir = (position - tmpPos).normalized;
     if (Vector2.Distance(tmpPos, position) > maxCycleByInterpolation)
     {
         oldPosition = position;
 
     }
     else while (Vector2.Distance(tmpPos, position) > 2)
     {
         oldPosition = position;
         scratchPoint(tmpPos);
         tmpPos += dir;
         i++;
         if (i > maxCycleByInterpolation)
         {
 
             break;
         }
     }
 }
 

 public void scratchPoint(Vector2 position)
 {
     Vector2 tmpPos = position - new Vector2(brushSize / 2, brushSize / 2);
     Vector2 cl = new Vector2((tmpPos.x + brushSize) - tex.width,
                              (tmpPos.y + brushSize) - tex.height);
     tmpPos = new Vector2(Mathf.Clamp(tmpPos.x, 0f, tmpPos.x),
                          Mathf.Clamp(tmpPos.y, 0f, tmpPos.y));
     Vector2 tmpBrushSize = new Vector2(Mathf.Clamp(brushSize, 0, brushSize - cl.x + 1),
                                        Mathf.Clamp(brushSize, 0, brushSize - cl.y + 1));
     if (tmpPos.x <= 0)
         tmpBrushSize.x = position.x + brushSize / 2;
     if (tmpPos.y <= 0)
         tmpBrushSize.y = position.y + brushSize / 2;
     tex.SetPixels((int)tmpPos.x, (int)tmpPos.y, (int)tmpBrushSize.x, (int)tmpBrushSize.y, brush, 0);
 }
 
 /// <summary>
 /// Scratchs main method. be careful, This method will scratch with interpolation if it's activated
 /// See the "interpolate" attribute
 /// </summary>
 /// <param name="position">Position, x:y on the texture</param>
 public void scratch(Vector2 position)
 {
     if (interpolateMovement)
         scratchInterpolated(position);
     else
         scratchPoint(position);
 }
 
 public void scratch(Vector3 position)
 {
     scratch(new Vector2((int)position.x, (int)position.y));
 }

}

On the Xcode Debugger it says the error is in our Assembly-CSharp.dll.s and the error it says is EXC_BAD_ACCESS (code=1, address = 0x0)

When we further debugged it, we pin pointed the code breaking when it reaches the public void ResetCovering() and hits the data = current.GetPixels32(); line.

Is GetPixels not readable by Xcode? Anything look wrong in my code? Is there a framework I am missing?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by phil_me_up · Feb 27, 2016 at 09:50 AM

Only briefly looked at the code as I'm on my phone, but I'd say your problem would be that 'current' might not assigned at the point you hit that function, and hence there is nothing to GetPixels on.

You really should be checking the validity of current and other references before you try using them, so stick a 'if(current != null)' in there.

You should be able to debug this in mono too, just check of that variable is null or not the first time that line is hit.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Political Peace Party Studios · Feb 27, 2016 at 08:38 PM 0
Share

Thanks so much for deciding to help us, I will be testing what you explained now and get back to you if it solves our problem. Thanks again!

avatar image Political Peace Party Studios · Feb 27, 2016 at 09:41 PM 0
Share

Hey Phil I think you may be right! Debugging this how you said too, I can see now in Xcode that "current" never gets assigned and that is why we are getting the BAD_ACCESS error. Any idea on what I should do so that it registers that? On Unity "current" is assigned. But on Xcode "current" is not assigned. What should I do to assign it? Thanks so much!

avatar image phil_me_up · Feb 28, 2016 at 12:36 AM 0
Share

We'll I'd start by double checking the order your functions are called. You might find that just safety checking as I described above, or wrapping in a try...catch statement will solve the problem (in the case that the function is called before initialisation is complete).

Another method I'd to expand the safety check and if current us found to be null, assign it as needed for your project.

Unfortunately I can't really tell you how to assign it as I'm not sure what it is your assigning and why. You might actually be better off investigating why this reference becomes null in the first place and prevent that from occurring (however, safety checks are always very much recommended anyway - they help keep your game stable!)

avatar image Political Peace Party Studios phil_me_up · Feb 28, 2016 at 07:43 AM 0
Share

Thanks so much for your help, now that I know this is the problem we can continue debugging (trial and error) style to figure out how to solve this. Thanks again for your time!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My app file size doubles that of what the console says it should... 1 Answer

Mix native Objective-C code and Unity3d 0 Answers

xcode build fail - Mach-o linker error 3 Answers

AWS in iOS not working 2 Answers

How to solve ITMS-90809: Deprecated API Usage? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges