コード例 #1
0
    private void GeneratePackedTexture()
    {
        var redChannelInput   = _channelRedTextureObjectField.value;
        var greenChannelInput = _channelGreenTextureObjectField.value;
        var blueChannelInput  = _channelBlueTextureObjectField.value;
        var alphaChannelInput = _channelAlphaTextureObjectField.value;
        var width             = _widthIntField.value;
        var height            = _heightIntField.value;

        var redChannelTexture   = (Texture2D)redChannelInput;
        var greenChannelTexture = (Texture2D)greenChannelInput;
        var blueChannelTexture  = (Texture2D)blueChannelInput;
        var alphaChannelTexture = (Texture2D)alphaChannelInput;

        // Create the composite texture
        Texture2D compositeTexture;

        if (_scaleToSpecificValueToggle.value)
        {
            compositeTexture = TexturePacker.GetCompositeTextureRgb(
                _nameIdentifierTextField.text,
                redChannelTexture,
                greenChannelTexture,
                blueChannelTexture,
                alphaChannelTexture,
                (int)_textureSizeVectorField.value.x,
                (int)_textureSizeVectorField.value.y);
        }
        else
        {
            compositeTexture = TexturePacker.GetCompositeTextureRgb(
                _nameIdentifierTextField.text,
                redChannelTexture,
                greenChannelTexture,
                blueChannelTexture,
                alphaChannelTexture);
        }

        // If inputs are valid, add their names to a list
        var validInputsNameList = new List <string>();

        if (redChannelTexture)
        {
            validInputsNameList.Add(redChannelTexture.name);
        }
        if (greenChannelTexture)
        {
            validInputsNameList.Add(greenChannelTexture.name);
        }
        if (blueChannelTexture)
        {
            validInputsNameList.Add(blueChannelTexture.name);
        }
        if (alphaChannelTexture)
        {
            validInputsNameList.Add(alphaChannelTexture.name);
        }

        // Find the common substring in the names and add the identifier
        var compositeTextureName = StringUtilities.GetCommonPrefix(validInputsNameList) + _nameIdentifierTextField.text;

        // Find the first valid input and create the path based on that
        var listOfTextureInputs = new List <Texture2D>()
        {
            redChannelTexture,
            redChannelTexture,
            redChannelTexture,
            alphaChannelTexture
        };
        var relativeCompositeTexturePath =
            Path.Combine(
                Path.GetDirectoryName(AssetDatabase.GetAssetPath(
                                          GetFirstValidTextureInput(listOfTextureInputs))),
                compositeTextureName);

        // Save the composite texture
        var hasSaved = false;

        if (_tgaToggle.value)
        {
            var newRelativeCompositeTexturePath = relativeCompositeTexturePath + _tgaToggle.label;
            var absoluteCompositeTexturePath    = Path.Combine(
                Directory.GetParent(Application.dataPath).FullName,
                newRelativeCompositeTexturePath);
            TextureUtilities.SaveTextureToPath(
                compositeTexture,
                absoluteCompositeTexturePath,
                TextureUtilities.TextureUtilitiesFormats.Tga);
            hasSaved = true;
        }
        if (_pngToggle.value)
        {
            var newRelativeCompositeTexturePath = relativeCompositeTexturePath + _pngToggle.label;
            var absoluteCompositeTexturePath    = Path.Combine(
                Directory.GetParent(Application.dataPath).FullName,
                newRelativeCompositeTexturePath);
            TextureUtilities.SaveTextureToPath(
                compositeTexture,
                absoluteCompositeTexturePath,
                TextureUtilities.TextureUtilitiesFormats.Png);
            hasSaved = true;
        }

        // Update the previews for the provided images
        _previewImageRedChannelVisualElement.style.backgroundImage   = redChannelTexture;
        _previewImageGreenChannelVisualElement.style.backgroundImage = greenChannelTexture;
        _previewImageBlueChannelVisualElement.style.backgroundImage  = blueChannelTexture;
        _previewImageAlphaChannelVisualElement.style.backgroundImage = alphaChannelTexture;

        // If a new texture has been saved to disk, display it and select in the Project Window
        if (hasSaved)
        {
            AssetDatabase.Refresh();
            compositeTexture.Apply();
            _previewImageResultVisualElement.style.backgroundImage = compositeTexture;
            Selection.activeObject = AssetDatabase.LoadAssetAtPath(relativeCompositeTexturePath, typeof(Texture2D));
        }
    }