Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Anti aliasing lost when resizing on web explorer

Discussion in 'Flash' started by CrisCL, Sep 9, 2013.

  1. CrisCL

    CrisCL

    Joined:
    Mar 9, 2013
    Posts:
    14
    Hi!

    I have a problem with my swf exported file, and I will appreciate some help about that..
    I have changed the html file, so it can rescale the swf file generated with unity, depending on user's window resolution.
    When I open the html file with my firefox, is OK, but when I resize the window of the firefox, the swf lose the Antialiasing.

    I have configured the antialiasing in the Start method:

    Code (csharp):
    1.  
    2. private int lastWidth, lastHeight;
    3.  
    4. void Start () {
    5.         // Set AntiAliasing Quality
    6.         QualitySettings.antiAliasing = 4;
    7.  
    8.         lastWidth = Screen.width;
    9.         lastHeight = Screen.height;
    10.         }
    11.  
    and in the Update method, when the window is resized:

    Code (csharp):
    1.  
    2. void Update () {
    3.  
    4.             // Set again AntiAliasing Quality          
    5.             QualitySettings.antiAliasing = 4;
    6.         }
    7.  
    But it does not work.

    Can someone please help me to reactivate the antialiasing when I resize the window of the swf content in html?
    Thanks!!

    Chris.
     
    Last edited: Sep 9, 2013
  2. CrisCL

    CrisCL

    Joined:
    Mar 9, 2013
    Posts:
    14
    Finally I have found a solution... :p

    Activate the antialiasing via AS3...

    Using the "Custom Splash Screen - Single SWF" project, I activate the antialiasing after a resize event:

    Code (csharp):
    1.  
    2. private var stage3D:Stage3D;
    3. private var context3D:Context3D;
    4. private var loadComplete:int;
    5.  
    6. public function CustomSplashScreenExample()
    7. {
    8.     addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    9.  
    10.     // Added the resize event
    11.     stage.addEventListener(Event.RESIZE, resizeListener);
    12.     loadComplete = 100;
    13. }
    14.  
    15. private function onUnityContentLoaderComplete(event:Event):void
    16. {
    17.     loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onUnityContentLoaderComplete);
    18.    
    19.     childSWF = addChild(loader.content);
    20.            
    21.     //Hide the content, stopping the inbuilt "Loading" text from appearing
    22.     childSWF.visible = false;
    23.    
    24.     unityContent = loader.content as IUnityContent;
    25.         unityContent.setContentHost(this);     
    26.            
    27.     // Get the 3D Context
    28.     stage3D = stage.stage3Ds[0];
    29.     stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContext3DCreate);
    30.     stage3D.requestContext3D();
    31.  
    32. }
    33.  
    34. // Configure the back buffer at first
    35. private function onContext3DCreate(e:Event):void
    36. {
    37.     context3D = stage3D.context3D;
    38.     context3D.enableErrorChecking = true;
    39.     context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 4, true);
    40.            
    41.     stage.addEventListener(Event.ENTER_FRAME, update);
    42.            
    43.     loadComplete = 0;          
    44. }
    45.        
    46. // Resize event
    47. private function resizeListener (e:Event):void
    48. {
    49.     loadComplete = 0;
    50. }
    51.  
    52. // Force antialiasing 3 times after a resize event
    53. private function update(e:Event):void
    54. {
    55.     if(context3D == null) return;
    56.            
    57.     // Setup the back buffer for the context
    58.     if (loadComplete < 2)
    59.     {
    60.         context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 4, true);
    61.         loadComplete++;
    62.     }
    63.            
    64.     context3D.clear();
    65.     context3D.present();
    66. }
    67.  
    This works!!
    :D
     
  3. DDowell

    DDowell

    Joined:
    Feb 8, 2012
    Posts:
    52
    I solved it by listening on stage resize and then setting the QualitySettings.antiAliasing again.

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Flash;
    5.  
    6. public class ChangeAALevel : MonoBehaviour
    7. {
    8.     private string _aalevel = "AA: ?";
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         _aalevel = "AA: " + QualitySettings.antiAliasing;
    13.  
    14.     //----------------------------------------------------------THE IMPORTANT STUFF BEGINS HERE
    15.         SetupResizeListener();
    16.  
    17.         ApplyAntiAliasing();
    18.     }
    19.  
    20.     private void SetupResizeListener()
    21.     {
    22.         ActionScript.Import("com.unity.UnityNative");
    23.         ActionScript.Import("flash.display.Stage");
    24.         ActionScript.Import("flash.events.Event");
    25.  
    26.         Action<object> del = HandleStageResized;
    27.  
    28.         ActionScript.Statement("UnityNative.stage.addEventListener(Event.RESIZE, {0})", del);
    29.     }
    30.  
    31.     /// <summary>
    32.     /// Flash event handler for stage resizing. We will update anti-aliasing here.
    33.     /// </summary>
    34.     /// <param name="flashEvent"></param>
    35.     private void HandleStageResized(object flashEvent)
    36.     {
    37.         ApplyAntiAliasing();
    38.     }
    39.    
    40.     /// <summary>
    41.     /// Apply the current anti-aliasing setting.
    42.     /// Needed due to flash dropping the setting on resize and also on startup.
    43.     /// </summary>
    44.     public static void ApplyAntiAliasing()
    45.     {
    46.         var aaToSet = QualitySettings.antiAliasing;
    47.  
    48.         QualitySettings.antiAliasing = 0; //To avoid bug when setting the AA to the same value as it already has.
    49.  
    50.         QualitySettings.antiAliasing = aaToSet;
    51.     }
    52.     //----------------------------------------------------------END OF IMPORTANT STUFF
    53.  
    54.     // For testing purposes only.
    55.     void Update ()
    56.     {
    57.         if (Input.GetKey(KeyCode.Keypad2))
    58.         {
    59.             QualitySettings.antiAliasing = 2;
    60.         }
    61.         else if (Input.GetKey(KeyCode.Keypad4))
    62.         {
    63.             QualitySettings.antiAliasing = 4;
    64.         }
    65.         else if (Input.GetKey(KeyCode.Keypad0))
    66.         {
    67.             QualitySettings.antiAliasing = 0;
    68.         }
    69.         _aalevel = "AA: " + QualitySettings.antiAliasing;
    70.     }
    71.  
    72.     // For verification by human eyes.
    73.     void OnGUI()
    74.     {
    75.         GUI.Label(new Rect(0,0,200,20), _aalevel);
    76.     }
    77. }
    78.