Creating Custom Shadows ——创建自定义shadow

Custom shadows are a Robolectric feature that allows you to make targeted changes in the way Android functions under test. This could be anything from capturing simply that a method was called, to inserting code that interacts with test objects, to doing nothing at all.

Custom shadows allow you to include shadow functionality in only some of your test code, as opposed to adding or modifying a Shadow in Robolectric source. They also allow your shadow to refer to domain specific context, like domain objects in your test classes.

Writing a Custom Shadow

Custom shadows are structured much the same as normal shadow classes. They must include the@Implements(AndroidClassName.class) annotation on the class definition. You can use the normal shadow implementation options, such as shadowing instance methods using @Implementation or shadowing constructors usingpublic void __constructor__(...).

@Implements(Bitmap.class) public class MyShadowBitmap { @RealObject private Bitmap realBitmap; private int bitmapQuality = -1; @Implementation public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) { bitmapQuality = quality; return realBitmap.compress(format, quality, stream); } public int getQuality() { return bitmapQuality; } } } 

Using a Custom Shadow

Custom Shadows get hooked up to Robolectric using the @Config annotation on the test class or test method, using the shadowsarray attribute. To use the MyShadowBitmap class mentioned in the previous section, you would annotate the test in question with@Config(shadows={MyShadowBitmap.class}), and to include multiple custom shadows:@Config(shadows={MyShadowBitmap.class, MyOtherCustomShadow.class}). This causes Robolectric to recognize and use your custom shadow when executing code against the class you shadowed.

However, the Robolectric.shadowOf() method will not work with custom shadows, as it has to be implemented in Robolectric for each shadow class. You can instead use Robolectric.shadowOf_() and cast the return value to the custom Shadow class you implemented.

Also, if you choose to shadow an Android class that already is shadowed in Robolectric, you will replace the Robolectric shadow. You could try inheriting from the Robolectric shadow if you still need the base shadow functionality.

转载于:https://www.cnblogs.com/yexiant/p/5692843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity URP中,点光源的自定义阴影需要通过脚本来实现。具体步骤如下: 1. 创建一个新的着色器,并设置其为使用“Universal Render Pipeline/Lit”着色器。 2. 在着色器中添加一个新的块,用于计算阴影: ```hlsl #pragma shader_feature __ _SHADOWS_SOFT #pragma shader_feature __ _SHADOWS_CASCADE float3 ComputeShadowCoord(float4 pos) { float3 shadowCoord = pos.xyz / pos.w; shadowCoord = shadowCoord * 0.5f + 0.5f; #if defined(_SHADOWS_SOFT) shadowCoord.z += 0.0005f; #endif #if defined(_SHADOWS_CASCADE) shadowCoord.z += 0.0001f * pos.w; #endif return shadowCoord; } ``` 3. 在Pass中添加以下代码,用于计算阴影: ```hlsl #ifdef LIGHTMAP_ON SHADOW_COORDS_LIGHTMAP #elif defined(SHADOWS_SCREEN) SHADOW_COORDS_SCREEN #else float3 shadowCoord = ComputeShadowCoord(UnityObjectToClipPos(v.vertex)); #endif ``` 4. 在Pass中添加以下代码,用于生成阴影: ```hlsl #if defined(_SHADOWS_SOFT) float shadow = ComputeSoftPointShadow(shadowCoord, _LightPosition, _LightRadius, _ShadowmapTexture, _ShadowStrength); #elif defined(_SHADOWS_CASCADE) float shadow = ComputeCascadePointShadow(shadowCoord, _CascadeShadowmapTexture, _CascadeShadowStrength, _CascadeShadowDistance); #else float shadow = ComputePointShadow(shadowCoord, _LightPosition, _ShadowmapTexture); #endif return shadow; ``` 其中,ComputePointShadow函数用于计算点光源的阴影。您可以通过调整参数来自定义阴影的外观和行为。 希望这能帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值