2018년 1월 20일 토요일

IOS11 Private api - exist specific application

- (BOOL)existAppContainer: (NSString*)bundleID
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0)
    {
        NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
        if ([container load])
        {
            Class appContainer = NSClassFromString(@"MCMAppContainer");
            id test = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleID withObject:nil];
            NSLog(@"%@",test);
            
            if (test)
            {
                return YES;
            }
        }
    }
    
    return NO;

}

IOS Private api - detect airplane mode

- (BOOL) isAirplainMode
{
    NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/AppSupport.framework"];
    BOOL success = [bundle load];
    BOOL airplain = NO;
    
    if(YES == success)
    {
        Class RadiosPreferences = NSClassFromString(@"RadiosPreferences");
        id radioPreferences = [[RadiosPreferences alloc] init];
        
        SEL selector = NSSelectorFromString(@"airplaneMode");
        if(YES == [radioPreferences respondsToSelector:selector])
        {
            airplain = [[radioPreferences valueForKey:@"airplaneMode"] boolValue];
        }
    }

    return airplain;

}

2018년 1월 8일 월요일

CommandBuffer를 사용한 Post Effect효과 처리

CommandBuffer를 사용하여 Gray Post Effect 효과를 적용해보는 Simple code...

일단 보여지는 결과는...

실행전
실행후

CommmandBuffer를 이용한 C#예제 소스

using UnityEngine;
using System.Collections;
using UnityEngine.Rendering;

public class TestCommandBuffer : MonoBehaviour
{
    // 작성해둔 Post Effect Shader를 연결합니다.(Inspect에서 연결)
    [SerializeField] 
    Shader shader; 

    void Start()
    {
        if (shader == null) 
        {
            return; 
        }

        var camera = Camera.main;
        if (camera == null) 
        {
            return; 
        }

        Material material = new Material(this.shader); // Shader Matrial 

        CommandBuffer commandBuffer = new CommandBuffer();

        // 적당한 이름의 TemporaryRT를 작성(임시적인 RenderTexture를 생성)
        int temp = Shader.PropertyToID("_Temp");
        commandBuffer.GetTemporaryRT(temp, -1, -1, 0, FilterMode.Bilinear);

        // 포스트 이펙트 처리
        // Blit : 첫번째 인자의 RenderTexture를 두번째 인자의 RenderTexture로 전달
        //        세번쨰 인자에 메터리얼이 지정되면 해당 메터리얼을 적용
        commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, temp);
        commandBuffer.Blit(temp, BuiltinRenderTextureType.CurrentActive, material);

        // TemporaryRT을 해제
        commandBuffer.ReleaseTemporaryRT(temp);

        // 카메라에 CommandBuffer 추가
        camera.AddCommandBuffer(CameraEvent.AfterEverything, commandBuffer);

        // RemoveCommandBuffer를 사용해서 CommandBuffer해제 처리
        //camera.RemoveCommandBuffer(CameraEvent.AfterEverything, commandBuffer);
    }
}



임의 객체를 생성해서 위 소스를 Add Component하고선
인스펙터에서 아래 Gray Shader 를 연결하고 실해하면 씬에 보여지는 내용이 Gray로 그려집니다.

Shader "Test/GrayImageEffectShader"
{
    Properties
    {
        _MainTex ("Texture"2D) = "white" {}
        _EffectAmount ("Effect Amount"Range (01)) = 1.0            // add
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
            
            sampler2D _MainTex;
            float4 _MainTex_ST;         // add
            fixed _EffectAmount;        // add

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                col.rgb = lerp(col.rgbdot(col.rgbfloat3(0.30.590.11)), _EffectAmount);
                return col;
            }
            ENDCG
        }
    }
}