Пример #1
0
    // Set sprite frame
    public static void SetSpriteFrame(Uni2DSprite a_rSpriteComponent, Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas, float a_fWidth, float a_fHeight, bool a_bUpdateMesh, bool a_bUpdateUV)
    {
        MeshFilter          rMeshFilter          = a_rSpriteComponent.GetComponent <MeshFilter>( );
        SkinnedMeshRenderer rSkinnedMeshRenderer = a_rSpriteComponent.GetComponent <SkinnedMeshRenderer>( );

        // The mesh to update
        Mesh rSpriteMesh = a_rSpriteComponent.EditableRenderMesh;

        if (rSpriteMesh != null)
        {
            if (a_bUpdateMesh)
            {
                rSpriteMesh = UpdateSpriteMeshSizeForAnim(a_rSpriteComponent, rSpriteMesh, a_fWidth, a_fHeight);
            }

            UpdateMaterialForAnim(a_rSpriteComponent, a_rTextureContainer, a_rTextureAtlas);

            if (a_bUpdateUV)
            {
                // Rebuild UVs
                rSpriteMesh.uv = BuildUVs(a_rTextureContainer,
                                          a_rSpriteComponent.SpriteData.renderMeshUVs,
                                          a_rTextureAtlas);
            }

            if (rSkinnedMeshRenderer != null && rSkinnedMeshRenderer.sharedMesh != rMeshFilter.sharedMesh)
            {
                rSkinnedMeshRenderer.sharedMesh = rSpriteMesh;
            }
        }
    }
    // Deep copy constructor
    public Uni2DEditorSpriteSettings(Uni2DEditorSpriteSettings a_rSpriteSettings)
    {
        this.textureContainer = new Texture2DContainer(a_rSpriteSettings.textureContainer);                     // Deep copy
        this.atlas            = a_rSpriteSettings.atlas;
        this.sharedMaterial   = a_rSpriteSettings.sharedMaterial;

        this.renderMesh             = a_rSpriteSettings.renderMesh;
        this.usePhysicBuildSettings = a_rSpriteSettings.usePhysicBuildSettings;

        this.spriteScale       = a_rSpriteSettings.spriteScale;
        this.pivotType         = a_rSpriteSettings.pivotType;
        this.pivotCustomCoords = a_rSpriteSettings.pivotCustomCoords;
        this.vertexColor       = a_rSpriteSettings.vertexColor;
        this.physicsMode       = a_rSpriteSettings.physicsMode;
        this.collisionType     = a_rSpriteSettings.collisionType;
        this.isKinematic       = a_rSpriteSettings.isKinematic;

        this.renderMeshAlphaCutOff            = a_rSpriteSettings.renderMeshAlphaCutOff;
        this.renderMeshPolygonizationAccuracy = a_rSpriteSettings.renderMeshPolygonizationAccuracy;
        this.renderMeshPolygonizeHoles        = a_rSpriteSettings.renderMeshPolygonizeHoles;

        this.alphaCutOff            = a_rSpriteSettings.alphaCutOff;
        this.extrusionDepth         = a_rSpriteSettings.extrusionDepth;
        this.polygonizationAccuracy = a_rSpriteSettings.polygonizationAccuracy;
        this.polygonizeHoles        = a_rSpriteSettings.polygonizeHoles;

        this.renderMeshGridHorizontalSubDivs = a_rSpriteSettings.renderMeshGridHorizontalSubDivs;
        this.renderMeshGridVerticalSubDivs   = a_rSpriteSettings.renderMeshGridVerticalSubDivs;

        this.skinQuality          = a_rSpriteSettings.skinQuality;
        this.boneInfluenceFalloff = a_rSpriteSettings.boneInfluenceFalloff;
    }
Пример #3
0
	// Set sprite frame
	public static void SetSpriteFrame(Uni2DSprite a_rSpriteComponent, Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas, float a_fWidth, float a_fHeight, bool a_bUpdateMesh, bool a_bUpdateUV)
	{
		MeshFilter rMeshFilter = a_rSpriteComponent.GetComponent<MeshFilter>( );
		SkinnedMeshRenderer rSkinnedMeshRenderer = a_rSpriteComponent.GetComponent<SkinnedMeshRenderer>( );

		// The mesh to update
		Mesh rSpriteMesh = a_rSpriteComponent.EditableRenderMesh;

		if( rSpriteMesh != null )
		{			
			if( a_bUpdateMesh )
			{
				rSpriteMesh = UpdateSpriteMeshSizeForAnim(a_rSpriteComponent, rSpriteMesh, a_fWidth, a_fHeight);
			}
	
			UpdateMaterialForAnim( a_rSpriteComponent, a_rTextureContainer, a_rTextureAtlas );
	
			if( a_bUpdateUV )
			{
				// Rebuild UVs
				rSpriteMesh.uv = BuildUVs( a_rTextureContainer,
					a_rSpriteComponent.SpriteData.renderMeshUVs,
					a_rTextureAtlas );
			}

			if( rSkinnedMeshRenderer != null && rSkinnedMeshRenderer.sharedMesh != rMeshFilter.sharedMesh )
			{
				rSkinnedMeshRenderer.sharedMesh = rSpriteMesh;
			}
		}
	}
Пример #4
0
    // Awake
    private void Awake()
    {
#if UNITY_EDITOR
        if (Application.isPlaying)
#endif
        {
            m_rCurrentFrameAtlas            = m_rSpriteSettings.atlas;
            m_rCurrentFrameTextureContainer = m_rSpriteSettings.textureContainer;
            m_iCurrentFrameTextureWidth     = (int)m_rSpriteData.spriteWidth;
            m_iCurrentFrameTextureHeight    = (int)m_rSpriteData.spriteHeight;
            spriteAnimation.Start(this);
        }

#if UNITY_EDITOR
        // Is a prefab instance
        else if (IsADuplicate( ))
        {
            //Debug.Log("Duplicate");
            Uni2DEditorResourceCopyUtils.DuplicateResources(this);
        }
#endif

        // Create the duplicata marker if needed
        if (m_oDuplicataMarker == null || m_oDuplicataMarker.IsADuplicate(this))
        {
            m_oDuplicataMarker = ScriptableObject.CreateInstance <DuplicataMarker>();
            m_oDuplicataMarker.Create(this);
        }
    }
Пример #5
0
	// Update material and texture
	private static void UpdateMaterialForAnim(Uni2DSprite a_rSpriteComponent, Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas)
	{
		Material oSpriteMaterial = a_rSpriteComponent.SpriteData.renderMeshMaterial;

		if(a_rTextureAtlas == null)
		{
			Material rAnimationMaterial = a_rSpriteComponent.RuntimeData.animationMaterial;

			if( rAnimationMaterial != null )
			{
				oSpriteMaterial = rAnimationMaterial;
			}
			else
			{
				// Instantiate the material to prevent other sprites using this material
				// to be animated too
				oSpriteMaterial = (Material) Material.Instantiate( oSpriteMaterial );
				a_rSpriteComponent.RuntimeData.animationMaterial = oSpriteMaterial;
			}

			oSpriteMaterial.mainTexture = a_rTextureContainer;
		}
		else
		{
			oSpriteMaterial = a_rTextureAtlas.GetMaterial( a_rTextureContainer.GUID );
		}
		
		Renderer rRenderer = a_rSpriteComponent.GetComponent<Renderer>();
		if(rRenderer != null)
		{
			rRenderer.sharedMaterial = oSpriteMaterial;
			rRenderer.material = oSpriteMaterial;
		}
	}
    // Removes duplicates and empty containers
    private Texture2DContainer[] SanitizeInputTextures(Texture2DContainer[] a_rTextureContainers)
    {
        int iUniqueTextureContainerCount = 0;
        int iTextureContainerCount       = a_rTextureContainers.Length;
        List <Texture2DContainer> oUniqueTextureContainersList = new List <Texture2DContainer>(iTextureContainerCount);

        // Iterate all texture containers...
        for (int iTextureContainerIndex = 0; iTextureContainerIndex < iTextureContainerCount; ++iTextureContainerIndex)
        {
            Texture2DContainer rTextureContainer = a_rTextureContainers[iTextureContainerIndex];

            // if not null and pointing to an existing texture (== not empty)...
            if (rTextureContainer != null && !string.IsNullOrEmpty(rTextureContainer.GUID) && rTextureContainer.Texture != null)
            {
                bool bIsDuplicate = false;

                // Look for duplicates
                for (int iUniqueTextureContainerIndex = 0; !bIsDuplicate && iUniqueTextureContainerIndex < iUniqueTextureContainerCount; ++iUniqueTextureContainerIndex)
                {
                    bIsDuplicate = rTextureContainer.GUID == oUniqueTextureContainersList[iUniqueTextureContainerIndex].GUID;
                }

                // No duplicate => add to list
                if (!bIsDuplicate)
                {
                    oUniqueTextureContainersList.Add(rTextureContainer);
                    ++iUniqueTextureContainerCount;
                }
            }
        }

        // List -> array
        return(oUniqueTextureContainersList.ToArray( ));
    }
Пример #7
0
    // Update material and texture
    private static void UpdateMaterialForAnim(Uni2DSprite a_rSpriteComponent, Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas)
    {
        Material oSpriteMaterial = a_rSpriteComponent.SpriteData.renderMeshMaterial;

        if (a_rTextureAtlas == null)
        {
            Material rAnimationMaterial = a_rSpriteComponent.RuntimeData.animationMaterial;

            if (rAnimationMaterial != null)
            {
                oSpriteMaterial = rAnimationMaterial;
            }
            else
            {
                // Instantiate the material to prevent other sprites using this material
                // to be animated too
                oSpriteMaterial = (Material)Material.Instantiate(oSpriteMaterial);
                a_rSpriteComponent.RuntimeData.animationMaterial = oSpriteMaterial;
            }

            oSpriteMaterial.mainTexture = a_rTextureContainer;
        }
        else
        {
            oSpriteMaterial = a_rTextureAtlas.GetMaterial(a_rTextureContainer.GUID);
        }

        Renderer rRenderer = a_rSpriteComponent.GetComponent <Renderer>();

        if (rRenderer != null)
        {
            rRenderer.sharedMaterial = oSpriteMaterial;
            rRenderer.material       = oSpriteMaterial;
        }
    }
Пример #8
0
 public Texture2DContainer(Texture2DContainer a_rTextureContainer)
 {
     if (a_rTextureContainer != null)
     {
         m_oTextureGUID = a_rTextureContainer.m_oTextureGUID;
         m_rTexture     = a_rTextureContainer.m_rTexture;
     }
 }
Пример #9
0
	public Texture2DContainer( Texture2DContainer a_rTextureContainer )
	{
		if( a_rTextureContainer != null )
		{
			m_oTextureGUID = a_rTextureContainer.m_oTextureGUID;
			m_rTexture     = a_rTextureContainer.m_rTexture;
		}
	}
Пример #10
0
    // Copy Constructor
    public Uni2DAnimationFrame(Uni2DAnimationFrame a_rFrameSource)
    {
        name         = a_rFrameSource.name;
        atlas        = a_rFrameSource.atlas;
        triggerEvent = a_rFrameSource.triggerEvent;
        a_rFrameSource.frameInfos.CopyTo(frameInfos);

        textureContainer = new Texture2DContainer(a_rFrameSource.textureContainer.GUID, atlas == null);
    }
Пример #11
0
    // Creates a new sprite settings with the given texture.
    // If the a_bPhysic argument is set to true, the settings will
    // depict a physical sprite
    public Uni2DEditorSpriteSettings(Texture2D a_rTexture2D, bool a_bPhysic = false)
    {
        this.textureContainer = new Texture2DContainer(a_rTexture2D, true);

        atlas = Uni2DEditorUtils.FindFirstTextureAtlas(textureContainer.GUID);

        // Set physic mode accordingly to given argument
        this.physicsMode = a_bPhysic
                        ? PhysicsMode.Dynamic
                        : PhysicsMode.NoPhysics;
    }
Пример #12
0
    public static Vector2[] BuildUVs(Texture2DContainer a_rTextureContainer, Vector2[] a_rRenderMeshUVs, Uni2DTextureAtlas a_rTextureAtlas)
    {
        // UV data
        Rect oUVRect          = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
        bool bIsUVRectFlipped = false;

        // No atlas
        if (a_rTextureAtlas == null)
        {
            oUVRect          = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
            bIsUVRectFlipped = false;
        }
        else if (!string.IsNullOrEmpty(a_rTextureContainer.GUID) && a_rTextureAtlas.GetUVs(a_rTextureContainer.GUID, out oUVRect, out bIsUVRectFlipped) == false)               // Atlas
        {
            // Not found in atlas!!!
            Debug.LogError("Uni2D: Atlas \'" + a_rTextureAtlas.name + "\' does not contain the texture with GUID \'" + a_rTextureContainer.GUID + "\'."
                           + "\nCheck your animation clip and atlas settings.", a_rTextureAtlas);
        }

        Vector2[] oMeshUVs     = a_rRenderMeshUVs;
        int       iVertexCount = oMeshUVs.Length;

        // UV normalization (computes mesh UVs coords in atlas UVs space)
        Vector2[] oNormalizedMeshUVs = new Vector2[iVertexCount];
        Vector2   f2UVOffset;           // Position (== min coords) of sprite UV rect in atlas. (0;0) if not atlased
        Vector2   f2UVRange;            // UV range (== max coords - position) in atlas. (1;1) if not atlased

        // If UV rect is not flipped...
        if (bIsUVRectFlipped == false)
        {
            f2UVOffset = new Vector2(oUVRect.xMin, oUVRect.yMin);
            f2UVRange  = new Vector2(oUVRect.xMax, oUVRect.yMax) - f2UVOffset;

            for (int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex)
            {
                oNormalizedMeshUVs[iUVIndex] = f2UVOffset + Vector2.Scale(f2UVRange, oMeshUVs[iUVIndex]);
            }
        }
        else         // UV rect is flipped
        {
            f2UVOffset = new Vector2(oUVRect.xMax, oUVRect.yMin);
            f2UVRange  = new Vector2(oUVRect.xMin, oUVRect.yMax) - f2UVOffset;

            for (int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex)
            {
                Vector2 f2UV = oMeshUVs[iUVIndex];

                // Need to swap UV coords
                oNormalizedMeshUVs[iUVIndex] = f2UVOffset + new Vector2(f2UVRange.x * f2UV.y, f2UVRange.y * f2UV.x);
            }
        }

        return(oNormalizedMeshUVs);
    }
Пример #13
0
	public static Vector2[ ] BuildUVs( Texture2DContainer a_rTextureContainer, Vector2[ ] a_rRenderMeshUVs, Uni2DTextureAtlas a_rTextureAtlas )
	{
		// UV data
		Rect oUVRect;
		bool bIsUVRectFlipped;

		// No atlas
		if( a_rTextureAtlas == null )
		{
			oUVRect = new Rect( 0.0f, 0.0f, 1.0f, 1.0f );
			bIsUVRectFlipped = false;
		}
		else if( !string.IsNullOrEmpty( a_rTextureContainer.GUID ) && a_rTextureAtlas.GetUVs( a_rTextureContainer.GUID, out oUVRect, out bIsUVRectFlipped ) == false )	// Atlas
		{
			// Not found in atlas!!!
			Debug.LogError( "Uni2D: Atlas \'" + a_rTextureAtlas.name + "\' does not contain the texture with GUID \'" + a_rTextureContainer.GUID + "\'."
				+ "\nCheck your animation clip and atlas settings.", a_rTextureAtlas );
		}

		Vector2[ ] oMeshUVs = a_rRenderMeshUVs;
		int iVertexCount    = oMeshUVs.Length;

		// UV normalization (computes mesh UVs coords in atlas UVs space)
		Vector2[ ] oNormalizedMeshUVs = new Vector2[ iVertexCount ];
		Vector2 f2UVOffset;	// Position (== min coords) of sprite UV rect in atlas. (0;0) if not atlased
		Vector2 f2UVRange;	// UV range (== max coords - position) in atlas. (1;1) if not atlased

		// If UV rect is not flipped...
		if( bIsUVRectFlipped == false )
		{
			f2UVOffset = new Vector2( oUVRect.xMin, oUVRect.yMin );
			f2UVRange  = new Vector2( oUVRect.xMax, oUVRect.yMax ) - f2UVOffset;

			for( int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex )
			{
				oNormalizedMeshUVs[ iUVIndex ] = f2UVOffset + Vector2.Scale( f2UVRange, oMeshUVs[ iUVIndex ] );
			}
		}
		else // UV rect is flipped
		{
			f2UVOffset = new Vector2( oUVRect.xMax, oUVRect.yMin );
			f2UVRange  = new Vector2( oUVRect.xMin, oUVRect.yMax ) - f2UVOffset;

			for( int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex )
			{
				Vector2 f2UV = oMeshUVs[ iUVIndex ];

				// Need to swap UV coords
				oNormalizedMeshUVs[ iUVIndex ] = f2UVOffset + new Vector2( f2UVRange.x * f2UV.y, f2UVRange.y * f2UV.x );
			}
		}

		return oNormalizedMeshUVs;
	}
Пример #14
0
    public string[] GetAllFramesTextureGUIDs( )
    {
        HashSet <string> oUniqueTextureGUIDs = new HashSet <string>( );

        foreach (Uni2DAnimationFrame rFrame in frames)
        {
            Texture2DContainer rTextureContainer = rFrame.textureContainer;
            if (rTextureContainer != null && !string.IsNullOrEmpty(rTextureContainer.GUID))
            {
                oUniqueTextureGUIDs.Add(rTextureContainer.GUID);
            }
        }

        string[] oTextureGUIDs = new string[oUniqueTextureGUIDs.Count];
        oUniqueTextureGUIDs.CopyTo(oTextureGUIDs);
        return(oTextureGUIDs);
    }
    // Revert settings
    public void RevertSettings( )
    {
        int iContainerCount = m_rTextureContainers != null ? m_rTextureContainers.Length : 0;

        textures = new Texture2DContainer[iContainerCount];

        // Deep copy
        for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex)
        {
            textures[iContainerIndex] = new Texture2DContainer(m_rTextureContainers[iContainerIndex]);
        }

        padding = m_iPadding;

        maximumAtlasSize = m_eMaximumAtlasSize;

        EditorUtility.SetDirty(this);
    }
Пример #16
0
    // Set the frame draw by the sprite
    private void SetSpriteFrame(Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas, int a_iWidth, int a_iHeight)
    {
        bool a_bUpdateMesh = false;

        if (m_iCurrentFrameTextureWidth != a_iWidth || m_iCurrentFrameTextureHeight != a_iHeight)
        {
            m_iCurrentFrameTextureWidth  = a_iWidth;
            m_iCurrentFrameTextureHeight = a_iHeight;
            a_bUpdateMesh = true;
        }

        bool a_bUpdateUV = false;

        if (m_rCurrentFrameAtlas != a_rTextureAtlas || (m_rCurrentFrameAtlas != null && m_rCurrentFrameTextureContainer != a_rTextureContainer))
        {
            m_rCurrentFrameTextureContainer = a_rTextureContainer;
            m_rCurrentFrameAtlas            = a_rTextureAtlas;
            a_bUpdateUV = true;
        }

        Uni2DSpriteUtils.SetSpriteFrame(this, a_rTextureContainer, a_rTextureAtlas, a_iWidth, a_iHeight, a_bUpdateMesh, a_bUpdateUV);
    }
Пример #17
0
	// Generate infos
	public void GenerateInfos( )
	{
		if( textureContainer != null )
		{
			// Ensure the frame is/is not referencing the texture if it has/hasn't have to
			// regarding to atlas setting
			if( textureContainer.IsKeepingTextureReference ^ atlas == null )	// XOR
			{
				// Keep reference if not atlasing; don't keep it if atlasing
				textureContainer = new Texture2DContainer( textureContainer.GUID, atlas == null );
			}

			if( textureContainer.Texture != null )
			{
				m_iTextureWidth  = textureContainer.Texture.width;
				m_iTextureHeight = textureContainer.Texture.height;
			}
		}
		else
		{
			m_iTextureWidth  = mc_iNoTextureWidth;
			m_iTextureHeight = mc_iNoTextureHeight;
		}
	}
Пример #18
0
    // Generate infos
    public void GenerateInfos( )
    {
        if (textureContainer != null)
        {
            // Ensure the frame is/is not referencing the texture if it has/hasn't have to
            // regarding to atlas setting
            if (textureContainer.IsKeepingTextureReference ^ atlas == null)                     // XOR
            {
                // Keep reference if not atlasing; don't keep it if atlasing
                textureContainer = new Texture2DContainer(textureContainer.GUID, atlas == null);
            }

            if (textureContainer.Texture != null)
            {
                m_iTextureWidth  = textureContainer.Texture.width;
                m_iTextureHeight = textureContainer.Texture.height;
            }
        }
        else
        {
            m_iTextureWidth  = mc_iNoTextureWidth;
            m_iTextureHeight = mc_iNoTextureHeight;
        }
    }
    // Apply settings
    public bool ApplySettings(bool a_bUpdateSprites = true)
    {
        bool bSuccess;

        Uni2DAssetPostprocessor.Enabled = false;
        {
            int         iContainerCount = textures.Length;
            Texture2D[] oTexturesToPack = new Texture2D[iContainerCount];
            for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex)
            {
                oTexturesToPack[iContainerIndex] = textures[iContainerIndex].Texture;
            }

            List <Uni2DTextureImporterSettingsPair> rTextureImporterSettings = Uni2DEditorSpriteBuilderUtils.TexturesProcessingBegin(oTexturesToPack);
            oTexturesToPack = null;

            // Look if the atlas is set properly regarding to texture sizes
            int iOversizedTextures = this.LookForOversizedTextures(textures, padding, maximumAtlasSize);
            if (iOversizedTextures == 0)
            {
                textures        = this.SanitizeInputTextures(textures);
                iContainerCount = textures.Length;

                string rAtlasGUID = Uni2DEditorUtils.GetUnityAssetGUID(this);
                Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;

                for (int iTextureIndex = 0, iTextureCount = m_rTextureContainers.Length; iTextureIndex < iTextureCount; ++iTextureIndex)
                {
                    rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, m_rTextureContainers[iTextureIndex].GUID);
                }

                m_rTextureContainers = new Texture2DContainer[iContainerCount];

                // Deep copy
                for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex)
                {
                    Texture2DContainer oTextureContainer = new Texture2DContainer(textures[iContainerIndex]);
                    m_rTextureContainers[iContainerIndex] = oTextureContainer;
                    rAssetTable.AddAtlasUsingTexture(rAtlasGUID, oTextureContainer.GUID);
                }

                rAssetTable.Save( );

                m_iPadding          = padding;
                m_eMaximumAtlasSize = maximumAtlasSize;

                bSuccess = Generate( );

                if (a_bUpdateSprites)
                {
                    Uni2DEditorSpriteBuilderUtils.UpdateSpriteInCurrentSceneAndResourcesAccordinglyToAtlasChange(this);
                }
            }
            else             // Some textures can't fit
            {
                bSuccess = false;
                Debug.LogWarning("Uni2D could not regenerate atlas '" + (this.gameObject.name) + "' properly: "
                                 + iOversizedTextures + " texture" + (iOversizedTextures > 1 ? "s are" : " is")
                                 + " too large to fit in the atlas.", this.gameObject);
            }

            Uni2DEditorSpriteBuilderUtils.TexturesProcessingEnd(rTextureImporterSettings);
        }
        Uni2DAssetPostprocessor.Enabled = true;

        EditorUtility.UnloadUnusedAssets( );

        return(bSuccess);
    }
Пример #20
0
	// Copy Constructor
	public Uni2DAnimationFrame(Uni2DAnimationFrame a_rFrameSource)
	{
		name = a_rFrameSource.name;
		atlas            = a_rFrameSource.atlas;
		triggerEvent     = a_rFrameSource.triggerEvent;
		a_rFrameSource.frameInfos.CopyTo( frameInfos );

		textureContainer = new Texture2DContainer( a_rFrameSource.textureContainer.GUID, atlas == null );
	}
Пример #21
0
	// Returns the number of textures which won't fit into the atlas
	private int LookForOversizedTextures( Texture2DContainer[ ] a_rTextureContainers, int a_iPadding, AtlasSize a_eMaximumAtlasSize )
	{
		float fMaximumAtlasSize = (float) a_eMaximumAtlasSize;
		float fPadding = (float) a_iPadding;
		int iOversized = 0;
		
		if( a_rTextureContainers != null )
		{
			foreach( Texture2DContainer rTextureContainer in a_rTextureContainers )
			{
				if( rTextureContainer != null )
				{
					Texture2D rTextureToPack = rTextureContainer;
					
					if( rTextureToPack != null && ( rTextureToPack.width + fPadding > fMaximumAtlasSize || rTextureToPack.height + fPadding > fMaximumAtlasSize ) )
					{
						++iOversized;
					}
				}
			}
		}
		return iOversized;
	}
Пример #22
0
	// Revert settings
	public void RevertSettings( )
	{
		int iContainerCount = m_rTextureContainers != null ? m_rTextureContainers.Length : 0;

		textures = new Texture2DContainer[ iContainerCount ];

		// Deep copy
		for( int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex )
		{
			textures[ iContainerIndex ] = new Texture2DContainer( m_rTextureContainers[ iContainerIndex ] );
		}
		
		padding = m_iPadding;
		
		maximumAtlasSize = m_eMaximumAtlasSize;

		EditorUtility.SetDirty( this );
	}
Пример #23
0
	// Apply settings
	public bool ApplySettings( bool a_bUpdateSprites = true )
	{
		bool bSuccess;

		Uni2DAssetPostprocessor.Enabled = false;
		{
			int iContainerCount = textures.Length;
			Texture2D[ ] oTexturesToPack = new Texture2D[ iContainerCount ];
			for( int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex )
			{
				oTexturesToPack[ iContainerIndex ] = textures[ iContainerIndex ].Texture;
			}
	
			List<Uni2DTextureImporterSettingsPair> rTextureImporterSettings = Uni2DEditorSpriteBuilderUtils.TexturesProcessingBegin( oTexturesToPack );
			oTexturesToPack = null;
	
			// Look if the atlas is set properly regarding to texture sizes
			int iOversizedTextures = this.LookForOversizedTextures( textures, padding, maximumAtlasSize );
			if( iOversizedTextures == 0 )
			{
				textures = this.SanitizeInputTextures( textures );
				iContainerCount = textures.Length;
	
				string rAtlasGUID = Uni2DEditorUtils.GetUnityAssetGUID( this );
				Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance;
	
				for( int iTextureIndex = 0, iTextureCount = m_rTextureContainers.Length; iTextureIndex < iTextureCount; ++iTextureIndex )
				{
					rAssetTable.RemoveAtlasUsingTexture( rAtlasGUID, m_rTextureContainers[ iTextureIndex ].GUID );
				}
	
				m_rTextureContainers = new Texture2DContainer[ iContainerCount ];
	
				// Deep copy
				for( int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex )
				{
					Texture2DContainer oTextureContainer = new Texture2DContainer( textures[ iContainerIndex ] );
					m_rTextureContainers[ iContainerIndex ] = oTextureContainer;
					rAssetTable.AddAtlasUsingTexture( rAtlasGUID, oTextureContainer.GUID );
				}
	
				rAssetTable.Save( );
	
				m_iPadding = padding;			
				m_eMaximumAtlasSize = maximumAtlasSize;
				
				bSuccess = Generate( );

				if( a_bUpdateSprites )
				{
					Uni2DEditorSpriteBuilderUtils.UpdateSpriteInCurrentSceneAndResourcesAccordinglyToAtlasChange( this );
				}
			}
			else // Some textures can't fit
			{			
				bSuccess = false;
				Debug.LogWarning( "Uni2D could not regenerate atlas '" + ( this.gameObject.name ) + "' properly: "
					+ iOversizedTextures + " texture" + ( iOversizedTextures > 1 ? "s are" : " is" )
					+ " too large to fit in the atlas.", this.gameObject );
			}
	
			Uni2DEditorSpriteBuilderUtils.TexturesProcessingEnd( rTextureImporterSettings );
		}
		Uni2DAssetPostprocessor.Enabled = true;

		EditorUtility.UnloadUnusedAssets( );

		return bSuccess;
	}
Пример #24
0
	// Removes duplicates and empty containers
	private Texture2DContainer[ ] SanitizeInputTextures( Texture2DContainer[ ] a_rTextureContainers )
	{
		int iUniqueTextureContainerCount = 0;
		int iTextureContainerCount       = a_rTextureContainers.Length;
		List<Texture2DContainer> oUniqueTextureContainersList = new List<Texture2DContainer>( iTextureContainerCount );

		// Iterate all texture containers...
		for( int iTextureContainerIndex = 0; iTextureContainerIndex < iTextureContainerCount; ++iTextureContainerIndex )
		{
			Texture2DContainer rTextureContainer = a_rTextureContainers[ iTextureContainerIndex ];

			// if not null and pointing to an existing texture (== not empty)...
			if( rTextureContainer != null && !string.IsNullOrEmpty( rTextureContainer.GUID ) && rTextureContainer.Texture != null )
			{
				bool bIsDuplicate = false;
				
				// Look for duplicates
				for( int iUniqueTextureContainerIndex = 0; !bIsDuplicate && iUniqueTextureContainerIndex < iUniqueTextureContainerCount; ++iUniqueTextureContainerIndex )
				{
					bIsDuplicate = rTextureContainer.GUID == oUniqueTextureContainersList[ iUniqueTextureContainerIndex ].GUID;
				}

				// No duplicate => add to list
				if( !bIsDuplicate )
				{
					oUniqueTextureContainersList.Add( rTextureContainer );
					++iUniqueTextureContainerCount;
				}
			}
		}

		// List -> array
		return oUniqueTextureContainersList.ToArray( );
	}
Пример #25
0
	public bool Equals( Texture2DContainer a_rAnotherContainer )
	{
		return !System.Object.ReferenceEquals( a_rAnotherContainer, null )
			&& string.Equals( m_oTextureGUID, a_rAnotherContainer.m_oTextureGUID )
			&& m_rTexture == a_rAnotherContainer.m_rTexture;
	}
Пример #26
0
	// Set the frame draw by the sprite
	private void SetSpriteFrame(Texture2DContainer a_rTextureContainer, Uni2DTextureAtlas a_rTextureAtlas, int a_iWidth, int a_iHeight)
	{
		bool a_bUpdateMesh = false;
		if(m_iCurrentFrameTextureWidth != a_iWidth || m_iCurrentFrameTextureHeight != a_iHeight )
		{
			m_iCurrentFrameTextureWidth  = a_iWidth;
			m_iCurrentFrameTextureHeight = a_iHeight;
			a_bUpdateMesh = true;
		}
		
		bool a_bUpdateUV = false;
		if(m_rCurrentFrameAtlas != a_rTextureAtlas || (m_rCurrentFrameAtlas != null && m_rCurrentFrameTextureContainer != a_rTextureContainer))
		{
			m_rCurrentFrameTextureContainer = a_rTextureContainer;
			m_rCurrentFrameAtlas            = a_rTextureAtlas;
			a_bUpdateUV = true;
		}
		
		Uni2DSpriteUtils.SetSpriteFrame(this, a_rTextureContainer, a_rTextureAtlas, a_iWidth, a_iHeight, a_bUpdateMesh, a_bUpdateUV);
	}
Пример #27
0
	// Default constructor
	public Uni2DAnimationFrame( )
	{
		textureContainer = new Texture2DContainer( null );
	}
Пример #28
0
 public bool Equals(Texture2DContainer a_rAnotherContainer)
 {
     return(!System.Object.ReferenceEquals(a_rAnotherContainer, null) &&
            string.Equals(m_oTextureGUID, a_rAnotherContainer.m_oTextureGUID) &&
            m_rTexture == a_rAnotherContainer.m_rTexture);
 }
Пример #29
0
	// Deep copy constructor
	public Uni2DEditorSpriteSettings( Uni2DEditorSpriteSettings a_rSpriteSettings )
	{
		this.textureContainer       = new Texture2DContainer( a_rSpriteSettings.textureContainer );	// Deep copy
		this.atlas                  = a_rSpriteSettings.atlas;
		this.sharedMaterial			= a_rSpriteSettings.sharedMaterial;
		
		this.renderMesh             = a_rSpriteSettings.renderMesh;
		this.usePhysicBuildSettings = a_rSpriteSettings.usePhysicBuildSettings;
		
		this.spriteScaleMode        = a_rSpriteSettings.spriteScaleMode;
		this.spriteScale            = a_rSpriteSettings.spriteScale;
		this.spriteScaleNotUniform  = a_rSpriteSettings.spriteScaleNotUniform;
		this.pivotType				= a_rSpriteSettings.pivotType;
		this.pivotCustomCoords		= a_rSpriteSettings.pivotCustomCoords;
		this.vertexColor            = a_rSpriteSettings.vertexColor;
		this.dimensionMode			= a_rSpriteSettings.dimensionMode;
		this.physicsMode            = a_rSpriteSettings.physicsMode;
		this.collisionType          = a_rSpriteSettings.collisionType;
		this.isKinematic            = a_rSpriteSettings.isKinematic;
		this.isTrigger				= a_rSpriteSettings.isTrigger;
	
		this.renderMeshAlphaCutOff            = a_rSpriteSettings.renderMeshAlphaCutOff;
		this.renderMeshPolygonizationAccuracy = a_rSpriteSettings.renderMeshPolygonizationAccuracy;
		this.renderMeshPolygonizeHoles = a_rSpriteSettings.renderMeshPolygonizeHoles;
	
		this.alphaCutOff            = a_rSpriteSettings.alphaCutOff;
		this.extrusionDepth         = a_rSpriteSettings.extrusionDepth;
		this.polygonizationAccuracy = a_rSpriteSettings.polygonizationAccuracy;
		this.polygonizeHoles        = a_rSpriteSettings.polygonizeHoles;
		
		this.onlyBorder = a_rSpriteSettings.onlyBorder;
		this.subdivide = a_rSpriteSettings.subdivide;
		this.subdivisionCount = a_rSpriteSettings.subdivisionCount;

		this.renderMeshGridHorizontalSubDivs = a_rSpriteSettings.renderMeshGridHorizontalSubDivs;
		this.renderMeshGridVerticalSubDivs   = a_rSpriteSettings.renderMeshGridVerticalSubDivs;

		this.skinQuality          = a_rSpriteSettings.skinQuality;
		this.boneInfluenceFalloff = a_rSpriteSettings.boneInfluenceFalloff;
	}
Пример #30
0
 // Default constructor
 public Uni2DAnimationFrame( )
 {
     textureContainer = new Texture2DContainer(null);
 }
Пример #31
0
	// Creates a new sprite settings with the given texture.
	// If the a_bPhysic argument is set to true, the settings will
	// depict a physical sprite
	public Uni2DEditorSpriteSettings( Texture2D a_rTexture2D, bool a_bPhysic = false )
	{
		this.textureContainer = new Texture2DContainer( a_rTexture2D, true );
		
		atlas = Uni2DEditorUtils.FindFirstTextureAtlas( textureContainer.GUID );
		
		// Set physic mode accordingly to given argument
		this.physicsMode = a_bPhysic
			? PhysicsMode.Dynamic
			: PhysicsMode.NoPhysics;
	}
Пример #32
0
	// Awake
	private void Awake()
	{
#if UNITY_EDITOR
		if( Application.isPlaying )
#endif
		{
			m_rCurrentFrameAtlas            = m_rSpriteSettings.atlas;
			m_rCurrentFrameTextureContainer = m_rSpriteSettings.textureContainer;
			m_iCurrentFrameTextureWidth     = (int)m_rSpriteData.spriteWidth;
			m_iCurrentFrameTextureHeight    = (int)m_rSpriteData.spriteHeight;
			spriteAnimation.Start( this );
		}
		
#if UNITY_EDITOR
		// Is a prefab instance
		else if( IsADuplicate( ) )
		{
			//Debug.Log("Duplicate");
			Uni2DEditorResourceCopyUtils.DuplicateResources( this );
		}
#endif

		// Create the duplicata marker if needed
		if(m_oDuplicataMarker == null || m_oDuplicataMarker.IsADuplicate(this))
		{
			m_oDuplicataMarker = ScriptableObject.CreateInstance<DuplicataMarker>();
			m_oDuplicataMarker.Create(this);
		}
	}